【问题标题】:Library/Caches path not found in iOS 8在 iOS 8 中找不到库/缓存路径
【发布时间】:2014-12-11 11:15:44
【问题描述】:

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

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"

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

【问题讨论】:

  • 投反对票的你能回答我的问题吗??

标签: objective-c iphone ipad ios7 ios8


【解决方案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];

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 2015-11-12
    • 2015-01-15
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    相关资源
    最近更新 更多