Scaling the Image [ Memory Issue ]
<p>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.</p>

<pre><code>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;
}
</code></pre>

<p>Please help on this.</p>

<p>Thanks,</p>

<p>Sagar</p>
