Таинственная проблема управления памятью в Core Graphics
Вот метод, который я использую в категории на UIImage, чтобы изменить его размер. Все работает хорошо, но похоже, что в этом методе (я даже не уверен на 100%, что он там есть) накоплена память. Я не эксперт, и мне уже было довольно сложно отследить это до функции CGContext с помощью Instruments.app
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.
Как я могу избавиться от этой утечки или чего-то еще? Что нужно исправить (или что я пропустил в коде)?