我正在尝试保存 UIImage,但它给了我一个错误。UIImage 是从 UIImagePickerController 中挑选出来的,然后通过NSData
生成它并调用writeToFile:
这个NSData
对象来保存。这样做时,我会得到一个错误。同样的方法可以完美地存储其他图像。我可以毫无问题地存储的图像从网站检索为字节数组,然后转换为 UIImage,然后保存。似乎该问题特定于来自UIImagePickerController
.
谁能告诉我如何解决这个问题?
似乎UIImagePickerController
保存的图像很好,但我仍然收到导致应用程序崩溃的错误:
5 月 1 日 10:38:05 Simon-BS-iPhone Diims[619]:ImageIO:JPEG Corrupt JPEG 数据:数据段过早结束
这是用于保存图像的代码:
+ (void)storeImage:(UIImage *)image name:(NSString *)name
{
NSString *pathForCache = [self pathForCache];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
NSString *jpegPath = [pathForCache stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpeg", name]];
NSError *error;
if ([imageData writeToFile:jpegPath options:NSDataWritingAtomic error:&error] == NO)
{
DLog(@"Could not save image: %@", jpegPath);
DLog(@"%@", error);
}
}
选择图像时这样调用:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
// Close controller
[picker dismissModalViewControllerAnimated:YES];
// Save image
self.selectedDevice.image = image;
[CDeviceManager localUpdateDevice:self.selectedDevice delegate:self];
}