导入超过800万像素的应用程序崩溃到uiimageview

时间:2015-04-29 05:29:16

标签: ios xcode memory-management uiimageview ios8.1

我们的应用在megapixels导入大于8 iPhone的图片时崩溃。有时候内存使用量很高。

顺便说一下,我们尝试导入的图像大于8百万像素而不是那么大4000 x 3000(1200万像素)。使用iPhone相机拍摄的照片为3264 x 2448(8百万像素)。

这是我们的记忆报告

导入800万像素图像时

enter image description here

导入1200万像素图像时

enter image description here

内存使用量差异很大。任何人都知道为什么内存使用量和解决方案存在巨大差异?

(我们将使用图片进行编辑,过滤,处理等)。

这是我们的代码

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *selectedImage=[info objectForKey:UIImagePickerControllerOriginalImage];

    //SCALING UIIMAGE

    UIImage *scaledImage=[[UIImage alloc]init];
    if ((selectedImage.size.width>3264) && (selectedImage.size.height>2448))
    {
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(selectedImage.size.width/2, selectedImage.size.height/2), NO, 0.0);
        NSLog(@" HEIGHT %f",selectedImage.size.height);
        NSLog(@" WIDTH %f",selectedImage.size.width);

        [selectedImage drawInRect:CGRectMake(0, 0,selectedImage.size.width/2 ,selectedImage.size.height/2 )];
       scaledImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        NSLog(@" HEIGHT %f",scaledImage.size.height);
        NSLog(@" WIDTH %f",scaledImage.size.width);


        VisualEffectImageVIew.image=scaledImage;
        BackgroundImageView.image=scaledImage;
        ForegroundImageView.image=scaledImage;
    }
    else
    {
    VisualEffectImageVIew.image=selectedImage;
    BackgroundImageView.image=selectedImage;
    ForegroundImageView.image=selectedImage;
    }
   if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
    {
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {


        [popoverController dismissPopoverAnimated:YES];

        [self popoverControllerDidDismissPopover:popoverController];
  }

}

1 个答案:

答案 0 :(得分:0)

_MGL9114.jpg是一个图像文件。

_MGL9114尺寸为3744px×5616px

如果我将此文件导入我的iOS项目并在UIView中渲染它。它将需要108MB内存。因为iOS会实时渲染UIImage,如果你的图像质量很高,它会使用高内存来缓存图像数据。 / p>

如果要减少它,则应在后端加载UIImage并输出压缩版本。它只需要23MB内存。

即使你想编辑图像,只需渲染压缩版本的效果。当用户完成编辑时,你处理后端的所有效果(不渲染原始版本)并将原始版本保存到用户的库

NSString * imageBundle = [[NSBundle mainBundle] pathForResource:@"_MGL9114" ofType:@"jpg"];
NSData * imageData = [[NSData alloc]initWithContentsOfFile:imageBundle];
UIImage * handleImage = [UIImage imageWithData:imageData];

UIGraphicsBeginImageContextWithOptions(CGSizeMake(handleImage.size.width/10, handleImage.size.height/10), NO, 0.0);
[handleImage drawInRect:CGRectMake(0, 0,handleImage.size.width/10 ,handleImage.size.height/10 )];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, handleImage.size.width/10, handleImage.size.height/10)];
[imageView setImage:newImage];

[self.view addSubview:imageView];