ios – CGImageRef的EXC_BAD_ACCESS错误

我有一个功能:

- (UIImage *) resizeImage:(UIImage *)image width:(CGFloat)resizedWidth height:(CGFloat)resizedHeight shouldCrop:(BOOL)crop
{
    CGImageRef imageRef = [image CGImage];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmap = CGBitmapContextCreate(NULL, resizedWidth, resizedHeight, 8, 4 * resizedWidth, colorSpace, (CGBitmapInfo) kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(bitmap, CGRectMake(0, 0, resizedWidth, resizedHeight), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);

    if (crop) // for SmallImage (tableviewcell)
    {
        CGRect cropRect = CGRectMake(0, CGImageGetHeight(ref) * 0.5 - ((float) kIvHeight * 0.5), (float) kIvWidth, (float) kIvHeight);
        ref = CGImageCreateWithImageInRect(ref, cropRect);
    }

    UIImage * result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);
    CGImageRelease(imageRef);
    return result;
}

行CGImageRelease(imageRef);导致我的应用程序崩溃与EXC_BAD_ACCESS
我删除该行似乎工作 – 但这不会导致内存泄漏?

最佳答案 您没有CGImageRef imageRef,因为您使用[image CGImage]获取它,因此您不需要释放它.

看一下这个:How do I release a CGImageRef in iOS

点赞