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

时间:2010-09-03 07:09:18

标签: iphone objective-c

在我的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.

在模拟器中它可以正常工作。

3 个答案:

答案 0 :(得分:0)

如果图像总数超过1k x 1k像素,那么我的猜测是你的设备内存不足。模拟器有更多的内存可用。在模拟器构建上运行内存分配工具,查看图像和合成操作实际消耗的数量,达到峰值。

答案 1 :(得分:0)

我的猜测是你必须在将图像传递给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];
    // ...
}

答案 2 :(得分:0)

发现错误......有点愚蠢的错误。 就是那条线

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

我不应该评论它。 现在一切正常。