iOS 8中找不到库/缓存路径

时间:2014-10-16 07:22:55

标签: objective-c iphone ipad ios7 ios8

我正在尝试将图像保存到我的本地资源路径中。我已经编写了以下代码来保存本地图像。

NSString *resoursePath=[[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Library/Caches"]];
NSString *filePath=[NSString stringWithFormat:@"%@/%@", resoursePath, fileName];
BOOL isDirectory=NO;
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory])
{
     NSError *error;
     if([self writeToFile:filePath options:NSDataWritingAtomic error:&error])
        NSLog(@"Successfully saved");
     else
        NSLog(@"%@", error);
}

它适用于iOS 7.但问题出现在iOS 8中。它在控制台中写出以下错误。

Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x7f7f8adb0cf0 {NSFilePath=/Users/tapas/Library/Developer/CoreSimulator/Devices/1F61698C-FAF4-4F27-9DA1-14B3F7227308/data/Containers/Bundle/Application/A38802B2-CC82-4220-BA2D-A0FD225016D7/Library/Caches/635471351432677268_iPhoneThumb.jpg, NSUserStringVariant=Folder, NSUnderlyingError=0x7f7f8ae0c100 "The operation couldn’t be completed. No such file or directory"

请建议我该怎么做。提前谢谢。

2 个答案:

答案 0 :(得分:1)

我认为您不想手动构建该目录。这里有一个类似的问题,可以帮助您指明正确的方向:Document Directory Path of iOS 8 Beta Simulator

答案 1 :(得分:1)

最后我找到了答案。这项工作在iOS 6,7以及8.只需要更改以下行。

NSString *resoursePath=[[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Library/Caches"]];

NSString *resoursePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];