所以我有一个功能,可以拍摄2张图像并将其合并。可能图像太大,因为当我尝试使用2张较小的图像时,它可以正常工作,但我不确定。因此,我在相机上拍摄了一张照片,然后尝试从相册中打开它。我选择图像,并使用此功能将其与另一个图像组合:

- (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage {

    UIImage *image = nil;

    float scale = 0.5f;

    CGSize newImageSize = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), MAX(firstImage.size.height, secondImage.size.height));

        NSLog(@"reached image by combining image");
    //crashes here when the image has been selected from an album (secondImage).
    // runs fine when the image has been taken from the camera. (secondImage).

    if (UIGraphicsBeginImageContextWithOptions != NULL) {
        UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]);
    } else {
        UIGraphicsBeginImageContext(newImageSize);
    }



    [firstImage drawAtPoint:CGPointMake(roundf((newImageSize.width-firstImage.size.width)/2),
                                        roundf((newImageSize.height-firstImage.size.height)/2))];

    UIImage *scaledImage =
    [UIImage imageWithCGImage:[secondImage CGImage]
                        scale:scale orientation:UIImageOrientationUp];

    [scaledImage drawAtPoint:CGPointMake(roundf((100)),
                                         roundf((100)))];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

当我到达这一行时:
if (UIGraphicsBeginImageContextWithOptions != NULL) {
    UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]);
} else {
    UIGraphicsBeginImageContext(newImageSize);
}

它无声地崩溃。我认为这可能是内存问题?
该方法也被异步调用。

最佳答案

在合并时停止崩溃之前,我必须将图像的大小减小3。太大了。

10-07 19:41
查看更多