将应用程序保存到库时iPhone应用程序崩溃

问题描述:

我的应用程序在我的iPhone上测试时出现问题。 我都合并到一个四象,并将其保存到图片库的方法:将应用程序保存到库时iPhone应用程序崩溃

- (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像素,那么我的猜测是您在设备上的内存不足。模拟器有更多的可用内存。在模拟器版本上运行内存分配工具,查看图像和合成操作实际消耗的峰值。

+0

图片均为320x460。 – Crazer 2010-09-10 06:50:54

我的猜测是,您必须在将图像传递到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]; 
    // ... 
} 
+0

嗨Gobra,图像被保存到图书馆,但我没有得到“保存的警报”,之后应用程序被冻结。 – Crazer 2010-09-10 06:48:32

+0

...和我上面发布的错误保持不变。 – Crazer 2010-09-10 06:49:52

找到了错误...有一种愚蠢的错误。 这是该行

// message = NSLocalizedString(@"Your image was saved", @""); 

我应该还没有评论出来。 现在一切正常。