开发者

Scaling the Image

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.


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

CGImageRelease(imgRef);
CGContextRelease(context);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜