The following graphical code tagged as Leak by iOS ARC

时光毁灭记忆、已成空白 提交于 2019-12-12 03:47:22

问题


 (UIImage *) cropToSquare:(UIImage *)_image 
 {
if(_image.size.height < _image.size.width)
{
    CGRect drawRect = CGRectMake(0, 0, _image.size.height, _image.size.height); 
    UIGraphicsBeginImageContext(drawRect.size);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGRect cropArea = CGRectMake (((_image.size.width - _image.size.height)/2), 0, _image.size.height, _image.size.height);
    CGContextTranslateCTM(currentContext, 0.0, _image.size.height);
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CGContextDrawImage(currentContext, drawRect, CGImageCreateWithImageInRect (_image.CGImage, cropArea));
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return cropped;
}
else
{
    return _image;
}

}

The line CGContextDrawImage(currentContext, drawRect, CGImageCreateWithImageInRect (_image.CGImage, cropArea)) tagged 100% leakage.

Are there anything I need to do CG related release myself?

thanks


回答1:


I believe that ARC only works for Cocoa objects, it doesn't work for Core* frameworks.

To fix your leak you need to change to the following:

CGImageRef myImage = CGImageCreateWithImageInRect (_image.CGImage, cropArea);
CGContextDrawImage(currentContext, drawRect, myImage);
CFImageRelease(myImage);

That could be a pretty big leak, depending on the size of your images.



来源:https://stackoverflow.com/questions/11853845/the-following-graphical-code-tagged-as-leak-by-ios-arc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!