简体   繁体   中英

iOS giving EXC_BAD_ACCESS for my function

I wrote a small function to zoom by 2x. But it is giving EXC_BAD_ACCESS error while I run it. Below is the code

- (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;
}

I am new to iOS. Please help.

Thanks

CGImageRef can't be autoreleased, it's Core Foundation type. Try to use CGImageRelease(scaledImage) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM