Objective-C UIImage存沙盒与从沙盒读取
// 保存图片并返回路径
- (NSString *)saveImage:(UIImage *)tempImage
{
NSString *image_fileType = @"";
if (UIImagePNGRepresentation(tempImage) == nil) {
image_fileType = @"JPEG";
}else{
image_fileType = @"PNG";
}
NSString *imageName = [NSString stringWithFormat:@"/%d.%@",1000000+(arc4random() % 999999999),image_fileType];
NSData * imageData = UIImagePNGRepresentation(tempImage);
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString * fullPathToFile = [documentsDirectory stringByAppendingString:imageName];
// and then we write it out
[imageData writeToFile:fullPathToFile atomically:NO];
return fullPathToFile;
}
// 根据路径获取对应的图片
-(UIImage *)readImage:(NSString *)fullPathToFile{
NSString * path = fullPathToFile;
NSData * data = [NSData dataWithContentsOfFile:path];
UIImage * image = [UIImage imageWithData:data];
return image;
}