调整uiimage大小时的高内存使用率

时间:2015-05-04 09:26:19

标签: ios objective-c iphone xcode ios8.1

调整大小并在rect中绘制UIImage时占用大量内存。

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

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


        //SCALING UIIMAGE
        UIImage *scaledImage=[[UIImage alloc]init];
        if (((selectedImageFromData.size.width>3264) && (selectedImageFromData.size.height>2448)) || ((selectedImageFromData.size.height>3264) && (selectedImageFromData.size.width>2448)))
        {
           CGFloat originalWidth=selectedImageFromData.size.width;
                CGFloat originalHeight=selectedImageFromData.size.height;
                CGFloat myWidth=2048;
                CGFloat widthRatio=myWidth/originalWidth;
                CGFloat dynamicHeight=widthRatio*originalHeight;

         CGImageRef CoreGraphicsImage=selectedImageFromData.CGImage;
        CGColorSpaceRef colorSpace = CGImageGetColorSpace(CoreGraphicsImage);
        CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(CoreGraphicsImage);
  CGImageGetBitsPerComponent(CoreGraphicsImage);

        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;
        }
        else
        {
            NSLog(@" HEIGHT %f",selectedImageFromData.size.height);
            NSLog(@" WIDTH %f",selectedImageFromData.size.width);

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


            [popoverController dismissPopoverAnimated:YES];

            [self popoverControllerDidDismissPopover:popoverController];
      }

    }

下面是一张图片,在图片中可以看到上面提到的代码使用了太多内存。想知道为什么它会使用太多内存并且会欣赏任何解决方案。

enter image description here

2 个答案:

答案 0 :(得分:0)

说到图像,我一直是Brad Larson's GPUImage的忠实粉丝,它可以做你想做的任何事情并使用GPU代替CPU,这会给你足够的力量来避免崩溃到期记忆压力。

而且它还不错。

This stackoverflow question也可以帮助您在完成项目设置后实现自己想要的目标。

希望这会有所帮助:)

答案 1 :(得分:0)

你正在使用CPU将数据绘制到一个帧中,我建议你在GPU上下文中使用CGImage进行绘制

public class Main {

    public static void main(String[] args) {
        Cell<Character> c = new Cell<>('c',null);
        Cell<Character> b = new Cell<>('b',c);
        Cell<Character> y = new Cell<>('y',b);
        Cell<Character> x = new Cell<>('x',y);
        Cell<Character> a = new Cell<>('a',b);


        Cell<Character> ls1 = x;
        Cell<Character> ls2 = a;



        ls2.next.first = 'z';
        ls1.next = ls1.next.next;
        for (Cell ptr = ls2; ptr != null; ptr = ptr.next) {
            ls1 = new Cell(ptr.first, ls1);
        }

        print(ls1);
        print(ls2);

    }

    public static void print(Cell list) {
        for (Cell ptr = list; ptr != null; ptr = ptr.next) {
            System.out.print(ptr.first);
        }
        System.out.println("");
    }

    static class Cell<T> {
        T first;
        Cell<T> next;

        Cell(T f, Cell<T> n) {
            first = f;
            next = n;
        }
    }

}