UIImage imageNamed not returning image object in iOS 11

383 Views Asked by At

The following code returns an image object from path and works previous to iOS 11:

NSString *path = [anotherPath stringByAppendingPathComponent:file];
UIImage *image = [UIImage imageNamed:path];

However in iOS 11, image returns null. Is this an iOS 11 bug?

2

There are 2 best solutions below

1
On

+[UIImage imageNamed:] is documented as taking a filename, not a path. If you want to load an image from a path, use +[UIImage imageWithContentsOfFile:].

If your path here is referring to something in a nested folder in your bundle, you can ask NSBundle for the path for the resource and then pass that to +[UIImage imageWithContentsOfFile:]. This would look like

NSString *path = [NSBundle.mainBundle pathForResource:@"foo" ofType:@"jpg" inDirectory:@"dir"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
0
On

As @drewster pointed out, the path was incorrect; I had a lower case character. However, I noticed iOS 8 ignored this.