Search code examples
xcodeios7ios8xcode6nsbundle

NSBundle UIImage nil iOS 7 specific


I created a framework using the Xcode 6 Cocoa Touch Framework target feature. When the framework is used in another app running on iOS 8, it works perfectly fine, loading images, picking up nibs-storyboards etc.. When I run the same app on an iOS 7 device, the code is picked up, view controllers are showing up, images that are set in the interface builder for the nib are showing up. But the images that are being set through code are not showing up.

The image is present in the app's contents (when I do a show package contents).

But its not getting picked up. Please see my code.

NSString *bundlePath = [NSString stringWithFormat:@"%@/Frameworks/TestFramework.framework", [[NSBundle mainBundle] bundlePath]];

NSBundle *frameworkBundle = [NSBundle bundleWithPath:bundlePath];
[frameworkBundle load];
NSString *imagePath = [NSString stringWithFormat:@"%@/Pic2.JPG", bundlePath];
UIImage *image = [UIImage imageNamed:imagePath];
NSLog(@"FrameworkBundle:%@", frameworkBundle);
NSLog(@"BundlePath:%@", bundlePath);
NSLog(@"ImagePath:%@",imagePath);
NSLog(@"Image:%@",image);
self.imageView3.image = image;

This is the output

2014-11-19 11:48:01.324 TestApp[95937:60b] FrameworkBundle:NSBundle </Users/nbkqkka/Library/Developer/CoreSimulator/Devices/A64CFDA9-EB0E-4B65-B119-A213CD500217/data/Applications/C942AA07-E416-48B3-AE96-C853DB349B64/TestApp.app/Frameworks/TestFramework.framework> (loaded)
    2014-11-19 11:48:01.324 TestApp[95937:60b] BundlePath:/Users/nbkqkka/Library/Developer/CoreSimulator/Devices/A64CFDA9-EB0E-4B65-B119-A213CD500217/data/Applications/C942AA07-E416-48B3-AE96-C853DB349B64/TestApp.app/Frameworks/TestFramework.framework
    2014-11-19 11:48:01.325 TestApp[95937:60b] ImagePath:/Users/nbkqkka/Library/Developer/CoreSimulator/Devices/A64CFDA9-EB0E-4B65-B119-A213CD500217/data/Applications/C942AA07-E416-48B3-AE96-C853DB349B64/TestApp.app/Frameworks/TestFramework.framework/Pic2.JPG
    2014-11-19 11:48:01.474 TestApp[95937:60b] Image:(null)

Solution

  • The documentation for imageNamed says:

    The name of the file. If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle.

    It doesn't say anything about loading from a full path.

    Can you use:

    + (UIImage *)imageWithContentsOfFile:(NSString *)path
    

    instead?