简体   繁体   English

如何在iPhone中裁剪或获取较小尺寸的UIImage而不会出现内存泄漏?

[英]How to crop or get a smaller size UIImage in iPhone without memory leaks?

I am using a navigation controller in which I push a tableview Controller as follows: 我正在使用导航控制器,在其中按如下方式推送tableview控制器:

TableView *Controller = [[TableView alloc] initWithStyle:UITableViewStylePlain]; TableView * Controller = [[TableView alloc] initWithStyle:UITableViewStylePlain]; [self.navigationController pushViewController:Controller animated:NO]; [self.navigationController pushViewController:Animated:NO]; [Controller release]; [控制器发布];

In this table view I am using following two methods to display images: 在此表视图中,我使用以下两种方法显示图像:

- (UIImage*) getSmallImage:(UIImage*) img
{
    CGSize size = img.size;
    CGFloat ratio = 0;
    if (size.width < size.height) {
        ratio = 36 / size.width;
    } else {
        ratio = 36 / size.height;
    }
    CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

    UIGraphicsBeginImageContext(rect.size);
    [img drawInRect:rect];

    return UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{

    //create a context to do our clipping in
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    //create a rect with the size we want to crop the image to
    //the X and Y here are zero so we start at the beginning of our
    //newly created context

    CGFloat X = (imageToCrop.size.width - rect.size.width)/2;
    CGFloat Y = (imageToCrop.size.height - rect.size.height)/2;


    CGRect clippedRect = CGRectMake(X, Y, rect.size.width, rect.size.height);
    //CGContextClipToRect( currentContext, clippedRect);



    //create a rect equivalent to the full size of the image
    //offset the rect by the X and Y we want to start the crop
    //from in order to cut off anything before them
    CGRect drawRect = CGRectMake(0,
                                 0,
                                 imageToCrop.size.width,
                                 imageToCrop.size.height);

    CGContextTranslateCTM(currentContext, 0.0, drawRect.size.height);
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    //draw the image to our clipped context using our offset rect
    //CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);


    CGImageRef tmp = CGImageCreateWithImageInRect(imageToCrop.CGImage, clippedRect);

    //pull the image from our cropped context
    UIImage *cropped = [UIImage imageWithCGImage:tmp];//UIGraphicsGetImageFromCurrentImageContext();
    CGImageRelease(tmp);
    //pop the context to get back to the default
    UIGraphicsEndImageContext();

    //Note: this is autoreleased*/
    return cropped;
}

But when I pop the Controller, the memory being used is not released. 但是当我弹出Controller时,正在使用的内存不会释放。 Is there any leaks in the above code used to create and crop images. 上面用于创建和裁剪图像的代码中是否存在任何泄漏。

Also are there any efficient method to deal with images in iPhone. 也有任何有效的方法来处理iPhone中的图像。 I am having a lot of images and facing major challeges in resolving the memory issues. 在解决内存问题时,我有很多图像并且面临很多挑战。

tnx in advance. 提前发送。

i dunno about that, but this might be a problem: 我不知道这一点,但这可能是一个问题:

return UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

the second line is never run, because the first one returns 第二行永远不会运行,因为第一行返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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