xcode 与 assetsd 的连接被中断或 assetsd 死亡(带有内存警告)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30137378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:07:17  来源:igfitidea点击:

Connection to assetsd was interrupted or assetsd died (with memory warnings)

iosiphonexcodememory-managementios8.1

提问by USERX2DX

We are trying to let users import picture from their albums(UIImagePickerController) and also we are scaling/resizing down images that are greater than 8 megapixels(iPhone standard).

我们正在尝试让用户从他们的相册中导入图片(UIImagePickerController),并且我们正在缩放/缩小大于 8 兆像素(iPhone 标准)的图像。

But every time the app crashes with Connection to assetsd was interrupted or assetsd diedand Received memory warningwarnings after or before importing picture.At times Received memory warningwarning pops up when still looking for picture to import in UIImagePickerController.

但是每次应用程序在导入图片之后或之前崩溃Connection to assetsd was interrupted or assetsd diedReceived memory warning发出警告时。有时Received memory warning仍然在寻找要导入的图片时弹出警告UIImagePickerController

Specially on iPhone 4S this is worse, please help us in optimising our code so that it runs without warnings and crashes on older devices like iPhone 4S or iPad 2.

特别是在 iPhone 4S 上,情况更糟,请帮助我们优化我们的代码,使其在 iPhone 4S 或 iPad 2 等旧设备上运行时不会出现警告和崩溃。

Let us know if we are doing anything wrong in scaling/resizing down image using CoreGraphics.(Because this is where huge memory is used).

如果我们在使用 CoreGraphics 缩放/缩小图像时做错了什么,请告诉我们。(因为这是使用巨大内存的地方)。

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        UIImage *selectedImage=[info objectForKey:UIImagePickerControllerOriginalImage];
        if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
        {
            [picker dismissViewControllerAnimated:YES completion:nil];
        }
        else
        {
            [popoverController dismissPopoverAnimated:YES];
            [self popoverControllerDidDismissPopover:popoverController];
        }

        // COMPRESSING IMAGE
        NSData   *selectedImageData=UIImageJPEGRepresentation(selectedImage, 0.1);
        UIImage *selectedImageFromData=[UIImage imageWithData:selectedImageData];

        // IMAGE ASPECT RATIO
        CGFloat originalWidth=selectedImageFromData.size.width;
        CGFloat originalHeight=selectedImageFromData.size.height;
        CGFloat myWidth=2048;
        CGFloat myHeight=2048;
        CGFloat widthRatio=myWidth/originalWidth;
        CGFloat heightRatio=myHeight/originalHeight;
        CGFloat dynamicWidth=heightRatio*originalWidth;
        CGFloat dynamicHeight=widthRatio*originalHeight;


        //SCALING UIIMAGE MORE THAN 8 MEGAPIXELS
        if (((selectedImageFromData.size.width>3264) && (selectedImageFromData.size.height>2448)) || ((selectedImageFromData.size.height>3264) && (selectedImageFromData.size.width>2448)))
        {



             // DATA FROM UIIMAGE TO CORE GRAPHICS
             CGImageRef CoreGraphicsImage=selectedImageFromData.CGImage;
            CGColorSpaceRef colorSpace = CGImageGetColorSpace(CoreGraphicsImage);
            CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(CoreGraphicsImage);
            CGImageGetBitsPerComponent(CoreGraphicsImage);


            // RESIZING WIDTH OF THE IMAGE
            if (originalWidth>originalHeight)
            {


            CGContextRef context=CGBitmapContextCreate(NULL, myWidth, dynamicHeight, CGImageGetBitsPerComponent(CoreGraphicsImage), CGImageGetBytesPerRow(CoreGraphicsImage), colorSpace, bitmapInfo);


            CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
            CGContextDrawImage(context, CGRectMake(0, 0, myWidth, dynamicHeight), CoreGraphicsImage);
            CGImageRef CGscaledImage=CGBitmapContextCreateImage(context);
                UIImage *CGLastimage = [UIImage imageWithCGImage: CGscaledImage];
                NSLog(@"%f",CGLastimage.size.width);
                NSLog(@"%f",CGLastimage.size.height);

                VisualEffectImageVIew.image=CGLastimage;
                BackgroundImageView.image=CGLastimage;
                ForegroundImageView.image=CGLastimage;
            }


            //RESIZING HEIGHT OF THE IMAGE
            if (originalHeight>originalWidth)
            {
                CGContextRef context=CGBitmapContextCreate(NULL, dynamicWidth, myHeight, CGImageGetBitsPerComponent(CoreGraphicsImage), CGImageGetBytesPerRow(CoreGraphicsImage), colorSpace, bitmapInfo);


                CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
                CGContextDrawImage(context, CGRectMake(0, 0, dynamicWidth, myHeight), CoreGraphicsImage);
                CGImageRef CGscaledImage=CGBitmapContextCreateImage(context);
                UIImage *CGLastimage = [UIImage imageWithCGImage: CGscaledImage];

                NSLog(@"%f",CGLastimage.size.width);
                NSLog(@"%f",CGLastimage.size.height);

                VisualEffectImageVIew.image=CGLastimage;
                BackgroundImageView.image=CGLastimage;
                ForegroundImageView.image=CGLastimage;

            }


        }
        else
        {
            NSLog(@" HEIGHT %f",selectedImageFromData.size.height);
            NSLog(@" WIDTH %f",selectedImageFromData.size.width);

        VisualEffectImageVIew.image=selectedImageFromData;
        BackgroundImageView.image=selectedImageFromData;
        ForegroundImageView.image=selectedImageFromData;
        }


    }

Memory Report

内存报告

when scrolling in UIImagePickerController

在 UIImagePickerController 中滚动时

http://i.stack.imgur.com/qxx62.png

http://i.stack.imgur.com/qxx62.png

when scaling/resizing UIImage

缩放/调整 UIImage 大小时

http://i.stack.imgur.com/ELCA6.png

http://i.stack.imgur.com/ELCA6.png

回答by xaphod

Two points.

两点。

First, you are not releasing the objects you create -- you are leaking huge amounts of memory. Regardless of whether you are using ARC, you must call CGContextRelease or CGImageRelease as appropriate for every CGCreatecall.

首先,您没有释放您创建的对象——您正在泄漏大量内存。无论您是否使用 ARC,您都必须为每个 CG Create调用适当地调用 CGContextRelease 或 CGImageRelease 。

Second, if you just want to resize, using coregraphics is overkill, use UIKit instead. In the code I use below, note the use of @autorelease to ensure that objects are cleaned up by ARC as soon as the context ends

其次,如果你只是想调整大小,使用 coregraphics 是多余的,请改用 UIKit。在我下面使用的代码中,请注意 @autorelease 的使用以确保对象在上下文结束时立即被 ARC 清理

- (UIImage *)fitImage:(UIImage *)image scaledToFillSize:(CGSize)size {
    // do not upscale

    @autoreleasepool {
        if( image.size.width <= size.width && image.size.height <= size.height )
            return image;

        CGFloat scale = MIN(size.width/image.size.width, size.height/image.size.height);
        CGFloat width = image.size.width * scale;
        CGFloat height = image.size.height * scale;

        CGRect imageRect;
        // center image
        if( scale != 1.0 ) { // avoid divide by zero?
            if( size.width/image.size.width < size.height/image.size.height ) {
                // height needs to be centered
                imageRect = CGRectMake(0, (size.height-height)/2, width, height);
            } else {
                // width needs to be centered
                imageRect = CGRectMake((size.width-width)/2, 0, width, height);
            }
        }

        UIGraphicsBeginImageContextWithOptions(size, YES, 0);
        [[UIColor CollageBorderUIColor] setFill]; // otherwise it's ugly black fill
        UIRectFill(CGRectMake(0, 0, size.width, size.height));
        [image drawInRect:imageRect];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
}