tags:

views:

26

answers:

1

Hello All,

I need to perform scaling operation on UIImage and following is same code. causing a memory leak.not able to debug what is getting wrong here.

UIImage* ScaleImageWithWH(UIImage* image, CGRect aRect) 
{
    int kMaxResolution = 1800; 

    int x;
    int y;

    x = ( image.size.width * aRect.origin.x ) / aRect.size.width;
    y = ( image.size.height * aRect.origin.y ) / aRect.size.height;

    CGImageRef imgRef =  CGImageCreateWithImageInRect (image.CGImage, CGRectMake (x,y,aRect.size.width,aRect.size.height));
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform transform = CGAffineTransformIdentity;

    CGRect bounds = CGRectMake(aRect.origin.x, aRect.origin.y, width, height);

    bounds.size.width = aRect.size.width;
    bounds.size.height = aRect.size.height;

    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));

    UIImageOrientation orient = image.imageOrientation;

    switch(orient) 
    {

        case UIImageOrientationUp: //default
            transform = CGAffineTransformIdentity;
            break;

        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
    }

    UIGraphicsBeginImageContext(bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(context, scaleRatio, -scaleRatio);
    CGContextTranslateCTM(context, 0, -height);

    CGContextConcatCTM(context, transform);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageCopy;
}

Please help on this.

Thanks,

Sagar

A: 

You must release the CGImageRef and the CGContextRef by using the following code

CGImageRelease(imgRef);
CGContextRelease(context);
Jeroen de Leeuw