UIImage imageWithContentsOfFile: not working on iOS8

When I test my application on iOS8 beta3 and beta5, I find an error related to [UIImage imageWithContentsOfFile:].

When image resources are stored in a subpackage of the main package, it returns nil if we initialize UIImage using the following method:

NSString *testBundlePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:testBundlePath];
NSString *path = [bundle pathForResource:@"play.png" ofType:nil];

UIImage *image = [UIImage imageWithContentsOfFile:path]; //iOS8 image = nil, iOS7 image != nil

But when image resources are stored in the main package, initialization of UIImage can be done using the following method:

NSString *path = [[NSBundle mainBundle] pathForResource:@"play.png" ofType:nil];

UIImage *image = [UIImage imageWithContentsOfFile:path]; //iOS8 and iOS7 image != nil

We detected this problem in iOS8 beta3 and still exists in iOS8 beta 5.

The Demo app directory structure was below:
      XXX.app
           |----test~ipad.bundle
                |----play.png
           |----test~iphone.bundle
                |----play.png
           |----play.png
           |----play~iphone.png
           |----play~ipad.png
         ......

- ? , iOS8. , . . , Apple , !

+4
4

, . , , - , . UUID , ( ). , [[NSFileManager defaultManager] currentDirectoryPath] , , .

, - :

NSString *image = @"myImage.png";

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cache = [paths objectAtIndex:0];
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", cache, image];
UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
+5

pathForResource:ofType: "" ( ). , pathForResource:ofType:inDirectory: .

0

Apple fixed this bug on iOS8.1, Cheers

0
source

just use

NSString *fullPath = [bundle pathForResource:name ofType:@"png"];
NSData *data = [NSData dataWithContentsOfFile:fullPath];
UIImage *image = [UIImage imageWithData:data];
0
source

All Articles