Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

objective c - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...