【发布时间】:2014-10-06 09:42:07
【问题描述】:
当我在 iOS8 beta3 和 beta5 上测试我的应用时,我发现了一个关于 [UIImage imageWithContentsOfFile:] 的错误。
当图片资源存储在主包的子包中时,如果我们通过以下方法初始化 UIImage,它将返回 nil:
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
但是当图片资源存放在main bundle中时,UIImage初始化可以通过以下方法成功:
NSString *path = [[NSBundle mainBundle] pathForResource:@"play.png" ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path]; //iOS8 and iOS7 image != nil
我们在iOS8 beta3下发现了这个问题,在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上的一个bug,所以很多app都使用bundle来组织资源,比如图片。但现在,这个漏洞会让成千上万的应用变得丑陋且难以使用。我们真心希望苹果能在下个版本修复这个bug,否则我所有的应用都得为这个bug开发一个新版本!
【问题讨论】:
-
你在这一行有问题 NSString *path = [bundle pathForResource:@"play.png" ofType:nil];将其更改为 NSString *path = [bundle pathForResource:@"play" ofType:"png"];或者你也可以试试 PNG
-
感谢您的回答,但 NSString *path = [bundle pathForResource:@"play.png" ofType:nil] 在 iOS7 和 iOS8 上运行良好,并且 NSString *path = [bundle pathForResource:@" play" ofType:"png"] 也无法在 iOS8 上运行。:(