3

I'm trying to convert a project, which has images, into a static library.

The code that gets the image is as follows:

[UIImage imageNamed:@"foo.png"]

When I include this library into another project, the image doesn't load. However, if I copy the images into the new project, then it does load.

Is there any way I can get this to work where the images are only contained in the library and I don't have to copy them over to my project?

By the way, my Header Search Paths contains the path to where these images are located in the library, if that makes any difference.

Senseful
  • 86,719
  • 67
  • 308
  • 465

3 Answers3

3

Just prepend the name of the bundle that contains your image to the image name:

[UIImage imageNamed:@"Myframework.bundle/MyImage"

This also works in Interface Builder, the preview may be broken but the image will be properly loaded.

If using CocoaPods (which I would recommend) make sure to use the resource_bundles option for your images and Nibs.

You can see a related answer here.

Community
  • 1
  • 1
Rivera
  • 10,792
  • 3
  • 58
  • 102
  • Your answer to this question is correct. But I want to ask this question: http://stackoverflow.com/questions/22869670/how-can-i-load-an-image-from-assets-car-compiled-version-of-xcassets-within-an – Tom Fishman Apr 04 '14 at 17:58
  • It doesn't work for me.I am trying to load image from bundle added in static library.Is there anyway I access xcassets in a bundle? – user1010819 Dec 23 '14 at 07:56
0

Had a similar situation to this and wrote a script to copy the files in to the .app at compile time. A similar fix to the one we use is described in the "Non-code assets for static libraries" section on this web page. This works but can cause some code signing errors. Another option is to create a second .bundle target for the resources described on this web page although for some reason I could not get the bundle to actually build. I am currently looking at writing a script to copy the resources in to a bundle at compile time and compile any .xib files to .nibs, this is also a possible solution you could look at.

Rob Gill
  • 327
  • 1
  • 10
0

A static library cannot contain bundle resource. So simply linking the .a file will not be enough. But you should be able to cross-reference the static library xcodeproj. Example

tidwall
  • 6,881
  • 2
  • 36
  • 47
  • But an image can be Base64 encoded and hard coded into a static library. – uchuugaka Mar 31 '14 at 01:33
  • @uchuugaka - An embedded base64 image cannot be loaded using [UIImage imageNamed:]. As I understand, the [UIImage imageNamed:] call locates an image from a precompiled Bundle. And a static library cannot contain Bundles. – tidwall Mar 31 '14 at 19:32