简体   繁体   English

UIImage 方法“drawInRect”的内存泄漏

[英]Memory leak with UIImage method "drawInRect"

I have question about correct using UIImage and method drawInRect.我对正确使用 UIImage 和方法 drawInRect 有疑问。

I use iPad 4,Xcode Version 6.1.1 , IOS 8.1.2 and I used ARC and I have tried without ARC我使用 iPad 4、Xcode 版本6.1.1 、IOS 8.1.2并且我使用了 ARC 并且我尝试过没有 ARC

So, I have image "1.jpg".所以,我有图像“1.jpg”。

Image Properties:图像属性:

Dimension: 7500 x 8871 pixels尺寸: 7500 x 8871 像素

Resolution: 72 pixels/inch分辨率: 72 像素/英寸

Color Space: RGB色彩空间: RGB

Alpha Channel: NO Alpha 通道:

I need to rescale the original image "1.jpg"我需要重新缩放原始图像“1.jpg”

I use this code:我使用这个代码:

UIImage *originalImage=[UIImage imageNamed:@"1.jpg"];
CGSize newSize=(CGSizeMake(4096, 4096));
originalImage=[self GetScaledImage : originalImage andSize: newSize];

//----Scaling Method----------------------// //----缩放方法----------------------//

-(UIImage *) GetScaledImage :(UIImage *) inputImage andSize :(CGSize) inputSize
{

 UIImage *newImage=[[[UIImage alloc] initWithCGImage:inputImage.CGImage] autorelease];    
 if (newImage)
 {        
     UIGraphicsBeginImageContext(inputSize);
     [newImage drawInRect: CGRectMake(0, 0, inputSize.width, inputSize.height)];
     newImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
 }

 return newImage;
}

My question:我的问题:

if I use newSize: (4096, 4096)如果我使用 newSize: (4096, 4096)

UIImage *originalImage=[UIImage imageNamed:@"1.jpg"];
CGSize newSize=(CGSizeMake(4096, 4096));

Memory now: 3.1 MB现在的内存: 3.1 MB

originalImage=[self GetScaledImage : originalImage andSize: newSize];

Memory now: 4.3 MB现在的内存: 4.3 MB

and it works correct它工作正常

but if I use newSize: (3000, 4096)如果我使用 newSize: (3000, 4096)

I have this:我有这个:

UIImage *originalImage=[UIImage imageNamed:@"1.jpg"];
CGSize newSize=(CGSizeMake(3000, 4096));

Memory now: 3.1 MB现在的内存: 3.1 MB

originalImage=[self GetScaledImage : originalImage andSize: newSize];

Memory now: 52 MB现在的内存: 52 MB

and it works incorrect它工作不正确

and magic : if I use newSize: (4096,3000) it works correct too but (3000,4096) works incorrect和魔法:如果我使用 newSize: (4096,3000) 它也可以正常工作,(3000,4096) 工作不正确

So, my question: How does it work?所以,我的问题是:它是如何工作的?

You can solve the problem by using the following method instead.您可以使用以下方法来解决问题。

+ (UIImage *)scaleImageWithData:(NSData *)data withSize:(CGSize)size
                          scale:(CGFloat)scale
                    orientation:(UIImageOrientation)orientation {
    CGFloat maxPixelSize = MAX(size.width, size.height);
    CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)data, nil);
    NSDictionary *options = @{(__bridge id)kCGImageSourceCreateThumbnailFromImageAlways:(__bridge id)kCFBooleanTrue,
                              (__bridge id)kCGImageSourceThumbnailMaxPixelSize:[NSNumber numberWithFloat:maxPixelSize]
                              };
    CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(sourceRef, 0, (__bridge CFDictionaryRef)options);
    UIImage *resultImage = [UIImage imageWithCGImage:imageRef scale:scale orientation:orientation];
    CGImageRelease(imageRef);
    CFRelease(sourceRef);
    
    return resultImage;
}

I suppose there is no magic. 我想没有魔术。 The CGImage object is not deallocated but owned. CGImage对象没有被释放而是被拥有。 For this, you need to set image.CGImage to some variable and then release it with 为此,您需要将image.CGImage设置为某个变量,然后使用

CFRelease(cgImage); CFRelease(cgImage);

Also try to clear graphics context after all processing done. 完成所有处理后,也请尝试清除图形上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM