此代码在iOS 8上失败,但它可以在iOS 7上运行
UIImage *imageTest = ...
NSString *file = @"../Documents/Test.png";
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: file];
[UIImagePNGRepresentation(imageTest) writeToFile: fullpath atomically: YES];
UIImage *image = [UIImage imageNamed: file];
在iOS 8上我需要使用它
NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Test.png"];
[UIImagePNGRepresentation(imageTest) writeToFile:file atomically:YES];
UIImage *image = [[UIImage alloc] initWithContentsOfFile: file];
...但是我必须重构我的项目以及使用相对于主要包的路径的第三方库。
在我看来,像"/PathToMainBundle/MyApp.app/../Documents/something"
这样的路径要么没有得到妥善解决,要么根本不被iOS 8所允许
该路径应与"/PathToMainBundle/Documents/something"
答案 0 :(得分:1)
在iOS8中,资源目录(App.app),数据目录(NSCachesDirectory,NSTemporaryDirectory,..)分别管理如下。
因此,您应该根据iOS8中的绝对路径修复代码。
UIImage *imageTest = ...
NSString *fullpath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"Test.png"];
[UIImagePNGRepresentation(imageTest) writeToFile: fullpath atomically: YES];
UIImage *image = [UIImage imageNamed: fullpath];