使用最简单的路径保存应用程序中的文档:
NSData *imageData = UIImagePNGRepresentation(newImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"cached"]];
NSLog(@"pre writing to file");
if (![imageData writeToFile:imagePath atomically:NO])
{
NSLog(@"Failed to cache image data to disk");
}
else
{
NSLog(@"the cachedImagedPath is %@",imagePath);
}
然后将imagePath保存在NSUserDefaults中的某个字典中,或者以您喜欢的方式保存,然后要检索它,只需执行以下操作:
NSString *theImagePath = [yourDictionary objectForKey:@"cachedImagePath"];
UIImage *customImage = [UIImage imageWithContentsOfFile:theImagePath];