Ios8 在iOS 8上写入/读取交叉双点路径失败

Ios8 在iOS 8上写入/读取交叉双点路径失败,ios8,nsdata,Ios8,Nsdata,这段代码在iOS 8上失败,尽管它在iOS 7上可以工作 UIImage *imageTest = ... NSString *file = @"../Documents/Test.png"; NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: file]; [UIImagePNGRepresentation(imageTest) writeToFile: fullp

这段代码在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根本不允许


该路径应与iOS8中的
“/PathToMainBundle/Documents/something”

相同,资源目录(App.App)、数据目录(NSCachesDirectory、NSTemporaryDirectory,…)分别进行管理,如下所示

  • iOS7目录层次结构
    • 应用程序UUID
      • 文件
      • 图书馆
        • 贮藏
        • 偏好
      • tmp
      • App.App
  • iOS8目录层次结构
    • 资源目录UUID
      • App.App
    • 数据目录UUID
      • 文件
      • 图书馆
        • 贮藏
        • 偏好
      • tmp
所以,您应该根据iOS8中的绝对路径修复代码

UIImage *imageTest = ...

NSString *fullpath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"Test.png"];

[UIImagePNGRepresentation(imageTest) writeToFile: fullpath atomically: YES];

UIImage *image = [UIImage imageNamed: fullpath];

这在iOS8之前一直适用于我:
NSString*文件=[[NSBundle mainBundle]路径资源:@“SomeFile”类型:@“pdf”]现在我甚至看不到应用程序包中的文件。@Jorgen但我需要先使用相对于主包的路径创建文件,因为我使用的是我说过的库。使用iOS8,我甚至在使用iExplorer或Xcode 6中的“设备”窗口查看设备时看不到该文件。完全糊涂了。