Javan
Javan

Reputation: 111

UIImage imageWithContentsOfFile: not working on iOS8

When I test my app on iOS8 beta3 and beta5, I found an bug that about [UIImage imageWithContentsOfFile:].

When image resources are stored in sub-bundle of a main bundle, it will return nil if we initialize UIImage via 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 the image resources are stored in main bundle, UIImage Initialization can be succeed through the following method:

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

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

We have found this problem under iOS8 beta3 and it still exists under 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
         ......

Does anyone have the same problem? I think this is a bug on iOS8, Therefore, many apps have used bundle to organize resources, such as images. But now, this bug will make thousands of apps become ugly and hard to use. We really hope that Apple can fix this bug in the next version, otherwise, all of my app will have to developing an new version for this bug!

Upvotes: 4

Views: 8244

Answers (4)

Jagie
Jagie

Reputation: 2220

just use

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

Upvotes: 0

Javan
Javan

Reputation: 111

Apple has fixed this bug on iOS8.1,Cheers

Upvotes: 0

AndrewH
AndrewH

Reputation: 51

It's not actually broken, you're just sending it the wrong path. I'm guessing that you are using the simulator, and have saved a file somewhere in your app and then made note of the full path to that file. However, when you rebuild your app the UUID for the installed app changes, and your saved path is no longer valid (even though the files are still there). You can see your current active path by checking [[NSFileManager defaultManager] currentDirectoryPath] Note that the value changes each time you rebuild and re-deploy your app.

To get to your actual file, you'll need to do something like this:

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];

Upvotes: 5

Husrat Mehmood
Husrat Mehmood

Reputation: 2310

pathForResource:ofType: only looks in the Resources folder (and localised folders). If you are placing files into dedicated folders in the bundle then you need to use pathForResource:ofType:inDirectory: and supply the directory name.

Upvotes: 0

Related Questions