无法在本地保存图像IOS 8

时间:2014-10-30 22:34:49

标签: ios8 xcode6

我将此代码用于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

1 个答案:

答案 0 :(得分:0)

'文件'文件夹已从iOS8更改。查看Apple tech note

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

NSString *resourcePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

OR(如前一个链接所述)

// Returns the URL to the application's Documents directory. - (NSURL *)applicationDocumentsDirectory{ return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; }

希望这有帮助!