一旦设备重新启动,UIImages就无法恢复

时间:2014-10-24 14:51:57

标签: ios objective-c uiimage ios8 nsdocumentdirectory

在iOS 8中,我使用代码

在设备上编写UIImages
 NSString *documentsDirectory = nil;
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 documentsDirectory = [paths objectAtIndex:0];
 NSString *pathString = [NSString stringWithFormat:@"%@/%@%ld",documentsDirectory, @"ProfileImage", (long)[[NSUserDefaults standardUserDefaults] integerForKey:@"PatientsId"]];
 NSData *imageData = UIImageJPEGRepresentation(image, 40);////I have replaced it with UIImagePNGRepresentation as well
 [imageData writeToFile: pathString atomically:YES];

写入文件成功。 如果我尝试使用该功能恢复图像,

    NSArray *documentDirectories =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                    NSUserDomainMask, YES);
// Get one and only document directory from that list
NSString *documentDirectory = [documentDirectories objectAtIndex:0]; 
NSString *pathString =[documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%ld", @"ProfileImage", (long)[[NSUserDefaults standardUserDefaults] integerForKey:@"PatientsId"]]] ;
UIImage *image = [UIImage imageWithContentsOfFile: pathString];

恢复图像。 但是,如果我重新启动设备,图像就不再存在了。函数[UIImage imageWithContentsOfFile:pathString]返回nil,如果我使用NSData * myData = [[NSData alloc] initWithContentsOfFile:pathString] ;, myData也是nil。

这个程序在iOS 8之前运行得很好,但是它不再正常工作了。有人可以指导我这有什么问题吗?

0 个答案:

没有答案