Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use images asset catalog in cocoapod library for iOS

I have a cocoapod library which contains assets in 2 formats:

  • a .storyboard
  • XCode asset catalog .xcassets (with images)

my podspec file contains the definition for the resource bundle:

s.resource_bundle = {'SparkSetup' => ['Resources/**/*.{xcassets,storyboard}']} 

and I have a separate target in the pod project to create a resource bundle by using those files + a plist file for that bundle.

thing is that when I use the pod in an app project - I can see the storyboard/xcassets files being in the pod target and I can access and run the storyboard easily but the images referenced in the storyboard (to the .xcassets file) are not found in run-time (but displayed correctly in IB).

Error I get is:

Could not load the "spinner" image referenced from a nib in the bundle with identifier "(null)" 

I do see a bundle file in the products directory. To instanciate VCs in the storyboard I use:

+(NSBundle *)getResourcesBundle {     NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"SparkSetup" withExtension:@"bundle"]];     return bundle; }   +(UIStoryboard *)getSetupStoryboard {     UIStoryboard *setupStoryboard = [UIStoryboard storyboardWithName:@"setup" bundle:[SparkSetupMainController getResourcesBundle]];     return setupStoryboard; } 

which seems to work well for finding the storyboard, but not for finding images in the .xcassets in the same bundle.

What am I doing wrong? how can I reference images from this storyboard/from code and be able to integrate this UI pod into any app?

Thanks!

like image 861
mindbomb Avatar asked Sep 15 '15 03:09

mindbomb


People also ask

How do I add an asset catalog in Xcode?

Choose File > New > File. Choose Resource > Asset Catalog, and click Next. Give the asset catalog a name, choose a location, and click Create.

What is assets Xcassets?

xcassets Catalog in Xcode. An asset catalog, simply put, is a single folder in Xcode that you use to organize your app's images, icons, colors, and more. Instead of adding individual images to Xcode's file organizer, you add assets neatly organized in a single catalog.


1 Answers

At least as of version 1.0.1 of Cocoapods, image asset catalogs are supported.

In my Swift 4.2 code, I used:

public static var GoogleIcon: UIImage {     let bundle = Bundle(for: self)     log.msg("bundle: \(bundle)")     return UIImage(named: "GoogleIcon", in: bundle, compatibleWith: nil)! } 

In my pod spec, I used:

s.resources = "SMCoreLib/Assets/*.xcassets" 

When I build using the simulator, the .car file does show up in the Framework:

cd /Users/<snip>/SMCoreLib.framework  ls Assets.car  Info.plist  SMCoreLib 
like image 189
Chris Prince Avatar answered Sep 23 '22 23:09

Chris Prince