Safari
Safari

Reputation: 11935

iOS Static Library with Image resources

I have a static library where I need some graphics resources as Images. In my class I would to add an image in this way:

[UIImage imageNamed:@"myImage.png"]

but it not work... how can I do it?

Upvotes: 1

Views: 5143

Answers (5)

Pavel
Pavel

Reputation: 1

Make a Framework target, not static library. Make sure "Copy Bundle Resources" exists in Build Phases for your Framework. Add your resource files there.

Then to find the file in the Framework bundle from an outside project I just do this:

NSArray<NSBundle *> * bundles = [NSBundle allFrameworks];
NSString * filePath;
for (int i = 0; i < bundles.count; i++) {

     filePath = [[bundles objectAtIndex:i] pathForResource:@"picture" ofType:@"png"];
     if (filePath != nil) break; // found
}

Upvotes: 0

Safari
Safari

Reputation: 11935

I found the solution ..

I explored the bundles directory (compiled) and I've noticed that my images (.png) they had all the iestensione .tiff

To avoid this I set the key HIDPI_COMBINE_IMAGES in my bundle buid setting as NO

..so I can recover my pictures as suggested by e1985

Upvotes: 1

e1985
e1985

Reputation: 6279

Once you have that bundle, you can access to the resources you have there like this:

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"YourBundle" ofType:@"bundle"];
NSString *imagePath = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"yourImage" ofType:@"png"];
UIImage *image= [[UIImage alloc] initWithContentsOfFile:imagePath];

Upvotes: 2

Guru
Guru

Reputation: 22042

In Xcode, 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.

Upvotes: 1

trojanfoe
trojanfoe

Reputation: 122391

You can't provide anything other than code with a static library. This is because .a files are a simple archive of object files, with the added ability under Mac/iOS of supporting multiple CPU architectures.

The only option you have is to create a Framework, which can provide code as well as other resources.

Upvotes: 2

Related Questions