在iOS 8上写入/读取具有交错双点的路径失败 [英] Writing/reading to paths with interleaved double dots fails on iOS 8
问题描述
此代码在iOS 8上失败,但它可以在iOS 7上运行
This code fails on iOS 8, although it would work on 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上我需要使用它
On iOS 8 I need to use this instead
NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Test.png"];
[UIImagePNGRepresentation(imageTest) writeToFile:file atomically:YES];
UIImage *image = [[UIImage alloc] initWithContentsOfFile: file];
...但是我必须重构我的项目以及与路径相关的第三方库到主捆绑。
...but then I have to refactor my project and a third party library that works with paths relative to the main bundle.
在我看来像/ PathToMainBundle / MyApp.app /../ Documents / something这样的路径
未正确解析,或iOS 8完全不允许
It seems to me that paths like "/PathToMainBundle/MyApp.app/../Documents/something"
are either not properly resolved, or not allowed at all by iOS 8
该路径应与相同/ PathToMainBundle / Documents / something
推荐答案
在iOS8中,资源目录(App.app),数据目录(NSCachesDirectory,NSTemporaryDirectory,..)分别管理如下。
In iOS8, Resource Directory(App.app), Data Directory(NSCachesDirectory, NSTemporaryDirectory,..) are managed separately as below.
- iOS7目录层次结构
- App UUID
- 文件
- 图书馆
- 缓存
- 偏好设置
- 资源目录UUID
- App.app
- 文件
- 图书馆
- 缓存
- 偏好设置
因此,您应该根据iOS8中的绝对路径修复代码。
So, you should fix code based on absolute path in iOS8.
UIImage *imageTest = ... NSString *fullpath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"Test.png"]; [UIImagePNGRepresentation(imageTest) writeToFile: fullpath atomically: YES]; UIImage *image = [UIImage imageNamed: fullpath];
这篇关于在iOS 8上写入/读取具有交错双点的路径失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- App UUID