2014-09-23 33 views
0

在iOS 8此代码失败,失败了,但它会在iOS 7写入/读取与交错的双点路径上的iOS 8

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

这为我工作最多iOS8上:'的NSString *文件= [[一个NSBundle mainBundle] pathForResource:@ “SomeFile” ofType:@ “PDF”];'现在我甚至无法看到文件应用程序包。 – Jorgen 2014-09-23 08:38:39

+0

@Jorgen但我需要首先使用相对于主包的路径创建文件,因为我正在使用的库如 – rraallvv 2014-09-23 08:41:48

+0

正如我所说的。使用iOS8时,我无法在使用iExplorer或Xcode 6中的设备窗口查看设备时看到该文件。完全困惑。 – Jorgen 2014-09-23 08:47:24

回答

1

在iOS8上,资源目录(应用程序。应用程序),数据目录(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];