这是我在 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。
我怎样才能摆脱这种泄漏或其他什么?需要释放什么(或者我在代码中遗漏了什么)?