iPhone app crashes when saving picture to library
I have a problem with my app when testing it on my iphone. I have a method that combines four images in one and saves it to the photo library:
- (UIImage *)combineImages{
UIImage *image1 = firstImgView.image;
UIImage *image2 = secondImgView.image;
UIImage *image3 = thirdImgView.image;
UIImage *image4 = fourthImgView.image;
UIGraphicsBeginImageContext(image1.size);
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
[imag开发者_JAVA百科e2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
[image3 drawInRect:CGRectMake(0, 0, image3.size.width, image3.size.height)];
[image4 drawInRect:CGRectMake(0, 0, image4.size.width, image4.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
//save actual design in photo library
- (void)savePicture{
UIImage *myImage = [self combineImages];
UIImageWriteToSavedPhotosAlbum(myImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), self);
}
//feedback if picture saving was successfull
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSString *message;
NSString *title;
if (!error) {
title = NSLocalizedString(@"Image saved", @"");
// message = NSLocalizedString(@"Your image was saved", @"");
} else {
title = NSLocalizedString(@"Error", @"");
message = [error description];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"Ok", @"")
otherButtonTitles:nil];
[alert show];
[alert release];
}
When I want to test it I get the following error:
Program received signal: “EXC_BAD_ACCESS”.
Data Formatters temporarily unavailable, will re-try after a 'continue'. (Not safe to call dlopen at this time.)
warning: Cancelling call - objc code on the current thread's stack makes this unsafe.
In the simulator it works fine.
If the images total over 1k x 1k pixels, then my guess is that you ran out of memory on the device. The Simulator has a ton more memory available. Run the memory allocations Instrument on the Simulator build and see how much your images and the compositing operation actually consume, peak.
My guess is that you must retain combined image before passing it to UIImageWriteToSavedPhotosAlbum. Combined image is autoreleased and it can be freed before the actual save is done. Try this:
//save actual design in photo library
- (void)savePicture{
UIImage *myImage = [self combineImages];
[myImage retain];
UIImageWriteToSavedPhotosAlbum(myImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), self);
}
//feedback if picture saving was successfull
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
[image release];
// ...
}
Found the error... kind of a dumb error. It was that line
// message = NSLocalizedString(@"Your image was saved", @"");
I should've not commented it out. Now everything works fine.
精彩评论