johngraham
johngraham

Reputation: 6491

Error loading pngs from a .bundle in Objective-c

I'm building a framework in my Xcode project. To build the framework, I created an aggregate target. The framework consists of the static library, header files, and bundle of .png assets, MyFrameworkResources.bundle. When I build the aggregate, everything is successfully created and the bundle exists in my Products directory.

Also in my Xcode project is a target Application. This application is for testing the framework products (static library, header files, and bundle). I've added the static library and MyFrameworkResources.bundle to the Target Dependencies of the target Application. I've also added MyFrameworkResources.bundle to Copy Bundle Resources in Build Phases of the target Application.

I can use all files from the static library just fine, but I get an error when trying to load resources from the bundle. This is how I'm loading assets from the bundle:

NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath];
NSString *frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"MyFrameworkResources.bundle"];
NSBundle *frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath];
NSLog(@"bundle: %@", frameworkBundle);

NSError *error;
[frameworkBundle loadAndReturnError:&error]; // <-- THIS RETURNS AN ERROR
NSLog(@"error: %@", error);
UIImage *image = [UIImage imageWithContentsOfFile:[frameworkBundle pathForResource:@"AnyImage" ofType:@"tiff"]]; // <-- EDIT, USE "tiff", NOT "png"

When I log the bundle, I get this:

bundle: NSBundle /Users/my_comp/Library/Application Support/iPhone Simulator/6.1/Applications/blah-blah-blah/MyApp.app/MyFrameworkResources.bundle (not yet loaded)

When I log the error, I get this:

error: Error Domain=NSCocoaErrorDomain Code=4 "The bundle “MyFrameworkResources” couldn’t be loaded because its executable couldn’t be located." UserInfo=0x7535ee0 {NSLocalizedRecoverySuggestion=Try reinstalling the bundle., NSLocalizedFailureReason=The bundle’s executable couldn’t be located., NSLocalizedDescription=The bundle “MyFrameworkResources” couldn’t be loaded because its executable couldn’t be located., NSBundlePath=/Users/my_comp/Library/Application Support/iPhone Simulator/6.1/Applications/blah-blah-blah/MyApp.app/MyFrameworkResources.bundle}

So to double check, I cd'd into /Users/my_comp/Library/Application Support/iPhone Simulator/6.1/Applications/blah-blah-blah/MyApp.app/ on my computer, and the MyFrameworkResources.bundle was actually there. So now I'm at a loss. I've tried cleaning the aggregate target, cleaning the target Application, deleting the target Application, and no luck.

Any idea why I can't load images from the bundle? (sorry for the lengthy description)

Fyi, I've been using this great tutorial as a guideline to build the framework and bundle.

Upvotes: 4

Views: 2183

Answers (1)

johngraham
johngraham

Reputation: 6491

Ah after struggling on this a bit more, I cd'd into MyFrameworkResources.bundle just to double check that all the assets were included. It turns out that Xcode renamed the .png files to .tiff files, and removed the retina assets. After further investigation, I discovered that Xcode automatically combines the retina and non-retina .png files into a "one multi-page TIFF". Here's a reference.

Solution, use ofType:@"tiff" instead of ofType:@"png":

UIImage *image = [UIImage imageWithContentsOfFile:[frameworkBundle pathForResource:@"AnyImage" ofType:@"tiff"]];

Upvotes: 7

Related Questions