これは、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 を使用しています。
どうすればこのリークを取り除くことができますか? 何を解放する必要がありますか (またはコードで見逃したものは何ですか)?