Core Graphics中的神秘内存管理问题

时间:2013-07-21 22:46:59

标签: ios memory-management core-graphics cgbitmapcontextcreate

这是我在UIImage上用于调整大小的类别的方法。一切都运作良好,但似乎这种方法中的东西(我甚至不是100%确定它在那里)正在积累记忆。我不是专家,我已经很难使用Instruments.app将其追溯到CGContext函数

http://bytolution.com/instruments_scrnsht.png

- (UIImage *)cropWithSquareRatioAndResolution:(CGFloat)resolution
{
    UIImage *sourceImg = (UIImage*)self;
    CGSize size = [sourceImg size];
    int padding = 0;
    int pictureSize;
    int startCroppingPosition;
    if (size.height > size.width) {
        pictureSize = size.width - (2.0 * padding);
        startCroppingPosition = (size.height - pictureSize) / 2.0;
    } else {
        pictureSize = size.height - (2.0 * padding);
        startCroppingPosition = (size.width - pictureSize) / 2.0;
    }
    if (resolution == 0) resolution = sourceImg.size.width;
    CGRect cropRect = CGRectMake(startCroppingPosition, padding, pictureSize, pictureSize);    
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0,resolution, resolution));
    CGImageRef imageRef = CGImageCreateWithImageInRect([sourceImg CGImage], cropRect);

    // Build a context that's the same dimensions as the new size
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                            newRect.size.width,
                                            newRect.size.height,
                                            CGImageGetBitsPerComponent(imageRef),
                                            0,
                                            CGImageGetColorSpace(imageRef),
                                            CGImageGetBitmapInfo(imageRef));

    CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh);
    CGContextDrawImage(bitmap, newRect, imageRef);
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:1.0 orientation:self.imageOrientation];

    // Clean up
    CGContextRelease(bitmap);
    CGImageRelease(newImageRef);
    CGImageRelease(imageRef);

    return newImage;
}

在上面的屏幕截图中,您可以看到每个拍摄的图像(以及由方法处理)后内存使用率如何变高。顺便说一下,我使用的是SDK 7和ARC。

我怎样才能摆脱这种泄漏或其他什么? 什么需要解除分配(或者我在代码中遗漏了什么)?

0 个答案:

没有答案