views:

725

answers:

4

in my iphone app, I am linking with a static library containing objective c files and images. Is it possible to load an image from a static library? I have tried

[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"]];

but obviously the image is not in the main bundle, it's in the static library and the NSBundle class seems only to offer access to the main bundle and to bundles with known paths. Is there a way to load an image from a static library on the iPhone?

A: 

It's not clear what you mean by "the image is [...] in the static library". Static libraries are plain files (with the .a extension) and contain archived object files. Bundles on the other hand are directory hierarchies (containing executables and other resources).

If you link a static library, the code from the library is included directly into your executable. No files are copied to the application bundle, so there's no way to copy the image.

If you have the image file with your static library, you can simply copy it to your application bundle by adding a copy files build phase to your target in Xcode.

Nikolai Ruhe
Thanks! I'm new to xcode and I needed a copy files build phase. That fixes it.
zaccox
+4  A: 

The simplest way is to store the image in an inline character array:

const char imageData[] = { 0x00, 0x01, 0xFF, ... };

And later when you need it:

UIImage *image = [UIImage imageWithData:[NSData dataWithBytesNoCopy:imageData length:sizeof(imageData) freeWhenDone:NO]];

You will have to convert your image's binary data (after saved as either PNG or JPEG) to the character array manually (or write a script to do so)

rpetrich
A: 

Do you use an Absolute Path in the static library copy files build phase and point it to the client application? I tried something like this and the images still cannot be accessed.

sfkaos
A: 

The simplest way is to store the image in an inline character array:

Yes, this is definitely the best way even a year later... Never ever.