Reputation: 8042
I am trying to save a UIImage but it gives me an error. The UIImage is picked from the UIImagePickerController and is then saved by making an NSData
out of it and call writeToFile:
on this NSData
object.
When doing so, I will get an error. The same method stores other images perfectly. The images which I have no problem storing retrieved from a website as a byte array and is then converted to a UIImage and is then saved.
It seems that the issue is specific for images from the UIImagePickerController
.
Can anybody tell me how I can fix this?
It seems that the images from the UIImagePickerController
is saved fine but I still get this error which causes the application to crash:
May 1 10:38:05 Simon-BS-iPhone Diims[619] : ImageIO: JPEG Corrupt JPEG data: premature end of data segment
This is the code used to save the image:
+ (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);
}
}
It is called like this when an image is selected:
- (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];
}
Upvotes: 0
Views: 789
Reputation: 1838
Try changing your code as follows if it works:-
- (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
}
Upvotes: 1