无法在IOS8本地保存图像

无法在IOS8本地保存图像,ios8,xcode6,Ios8,Xcode6,我有这段代码在IOS7上运行,它检查设备中是否存在映像,如果不存在,则在本地下载 现在在IOS8上什么也救不了,有人能帮我吗 //folder where save NSString *ImagesPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:

我有这段代码在IOS7上运行,它检查设备中是否存在映像,如果不存在,则在本地下载

现在在IOS8上什么也救不了,有人能帮我吗

//folder where save
     NSString *ImagesPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

 // check if image exist
 NSString* foofile = [ImagesPath stringByAppendingPathComponent:nombreImagenLocal];
 BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];

 // check if image exist locally
 if (!fileExists){
     NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:rutaCompletaLogo]];

 //if not, i save it
 if (data) {

     // url where is saved
     NSString *cachedImagePath = [ImagesPath stringByAppendingPathComponent:nombreImagenLocal];

     if ([data writeToFile:cachedImagePath atomically:YES]) {

 NSLog(@"Downloaded file saved to: %@", cachedImagePath);

 }// end

“文档”文件夹的路径已从iOS8更改。检查

请确保不要使用硬编码值。使用API提供的方法:

NSString *resourcePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
或如前一链接所述

//返回应用程序文档目录的URL。 -NSURL*应用程序文档目录{ 返回[[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject]; }


希望这有帮助

改为使用writeToFile:options:error:并为其指定一个NSError指针。然后检查N错误以查看错误。写入错误错误域=NSCOCAERRORDOMAIN代码=4操作无法完成。错误4。UserInfo=0x7f8170f37710{NSFilePath=/Users/BEhost/Library/Developer/CoreSimulator/Devices/66E03424-9959-473C-9912-BEAD2BE9C8A4/data/Containers/data/Application/79C7F75A-7A3C-4C26-88CE-3FC7B3DD12D2/Documents/,NSUserStringVariant=Folder,NSUnderlyingError=0x7f817315c590操作无法完成。没有此类文件或目录}