UIImage imageWithContentsOfFile:不适用于iOS8

Jav*_*van 4 beta uiimage ios8

当我在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
Run Code Online (Sandbox Code Playgroud)

但是当图像资源存储在主束中时,UIImage Initialization可以通过以下方法成功:

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

UIImage *image = [UIImage imageWithContentsOfFile:path]; //iOS8 and iOS7 image != nil
Run Code Online (Sandbox Code Playgroud)

我们在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
         ......
Run Code Online (Sandbox Code Playgroud)

有没有人有同样的问题?我认为这是iOS8上的一个错误,因此,许多应用程序使用bundle来组织资源,例如图像.但是现在,这个bug将使成千上万的应用程序变得丑陋且难以使用.我们真的希望Apple可以在下一个版本中修复这个bug,否则,我的所有app都必须为这个bug开发一个新版本!

And*_*ewH 5

它实际上并没有被破坏,你只是发送错误的路径.我猜你正在使用模拟器,并在你的应用程序的某个地方保存了一个文件,然后记下了该文件的完整路径.但是,当您重建应用程序时,已安装应用程序的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];
Run Code Online (Sandbox Code Playgroud)