将图片保存到库时iPhone应用程序崩溃
我在iphone上测试应用程序时遇到问题。 我有一种方法,可以将四幅图像合并为一幅,并将其保存到照片库中:将图片保存到库时iPhone应用程序崩溃,iphone,objective-c,Iphone,Objective C,我在iphone上测试应用程序时遇到问题。 我有一种方法,可以将四幅图像合并为一幅,并将其保存到照片库中: - (UIImage *)combineImages{ UIImage *image1 = firstImgView.image; UIImage *image2 = secondImgView.image; UIImage *image3 = thirdImgView.image; UIImage *image4 =
- (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)];
[image2 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];
}
当我想要测试它时,我得到以下错误:
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.
在模拟器中,它工作正常。如果图像总数超过1k x 1k像素,那么我猜您的设备内存不足。模拟器有更多的可用内存。在模拟器构建上运行内存分配工具,查看图像和合成操作实际消耗的峰值。我的猜测是,在将组合图像传递给UIImageWriteToSavedPhotosAlbum之前,必须保留组合图像。合并后的图像将自动删除,并且可以在实际保存之前将其释放。试试这个:
//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];
// ...
}
发现错误。。。有点愚蠢的错误。 就是那条线
// message = NSLocalizedString(@"Your image was saved", @"");
我不应该把它评论掉。
现在一切正常。Hi Gobra,图像已保存到库中,但我没有收到“已保存警报”,应用程序随后被冻结。