我正在尝试保存UIImage,但它给了我一个错误。 UIImage是从UIImagePickerController中挑选出来的,然后通过使用NSData
来保存,并在此writeToFile:
对象上调用NSData
。
这样做时,我会收到错误。相同的方法完美地存储其他图像。我没有问题的图像存储从网站检索为字节数组,然后转换为UIImage然后保存。
该问题似乎特定于UIImagePickerController
的图像。
有谁能告诉我如何解决这个问题?
似乎来自UIImagePickerController
的图片保存得很好,但我仍然遇到导致应用程序崩溃的错误:
5月1日10:38:05 Simon-BS-iPhone Diims [619]:ImageIO:JPEG 损坏的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];
}
答案 0 :(得分:1)
如果有效,请尝试按如下方式更改代码: -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
// Save image self.selectedDevice.image = image;
[CDeviceManager localUpdateDevice:self.selectedDevice delegate:self];
// Close controller [picker dismissModalViewControllerAnimated:YES];
//dismiss after saving the image
}