iOS为我的功能提供EXC_BAD_ACCESS

时间:2012-06-13 09:16:50

标签: ios exc-bad-access

我写了一个小功能来缩放2倍。但是在运行它时会出现EXC_BAD_ACCESS错误。以下是代码

- (CGImageRef)CGImageScale2x:(CGImageRef)imgRef
{
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGRect imgRect = CGRectMake(0, 0, width, height);
    CGAffineTransform transform = CGAffineTransformMakeScale(2.0, 2.0);
    CGRect scaledRect = CGRectApplyAffineTransform(imgRect, transform);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bmContext = CGBitmapContextCreate(NULL,
                                                   scaledRect.size.width,
                                                   scaledRect.size.height,
                                                   8,
                                                   0,
                                                   colorSpace,
                                                   kCGImageAlphaPremultipliedFirst);
    CGContextSetAllowsAntialiasing(bmContext, FALSE);
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationNone);
    CGColorSpaceRelease(colorSpace);
    CGContextScaleCTM(bmContext, 2.0, 2.0);
    CGContextDrawImage(bmContext, CGRectMake(0, 0,
                                             scaledRect.size.width,
                                             scaledRect.size.height),
                       imgRef);

    CGImageRef scaledImage = CGBitmapContextCreateImage(bmContext);
    CFRelease(bmContext);
    [(id)scaledImage autorelease];

    return scaledImage;
}

我是iOS新手。请帮忙。

由于

1 个答案:

答案 0 :(得分:1)

CGImageRef无法自动发布,它是Core Foundation类型。请尝试使用CGImageRelease(scaledImage)