如何释放声明为方法并传递给另一个方法的对象? [英] How to release an object declared into a method and passed to another method?

查看:102
本文介绍了如何释放声明为方法并传递给另一个方法的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下如何释放UIImage对象图片的任何想法:

Any idea on how to release the UIImage object picture in this case:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];
    UIImage *picture = [[UIImage alloc] init];
    if (payload.size.width != kAppIconHeight && payload.size.height != kAppIconHeight)
    {
        CGSize itemSize = CGSizeMake(kAppIconHeight, kAppIconHeight);
        UIGraphicsBeginImageContext(itemSize);
        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        [payload drawInRect:imageRect];
        picture = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    else
    {
        picture = payload;
    }

    self.activeDownload = nil;
    [payload release];

    self.imageConnection = nil;

    [delegate ThumbDidLoad:self.indexPathInTableView Image:picture];
}

Thx帮助,

Stephane

推荐答案

我很难理解为什么你的picture变量有一个alloc init。我同意早期使用自动释放的答案,但可能更像是:

I'm having a hard time understanding why your "picture" variable has an alloc init at all. I agree with earlier answers to use autorelease, but perhaps something more like:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    UIImage *payload = [UIImage imageWithData:self.activeDownload];
    UIImage *picture = nil;
    if (payload.size.width != kAppIconHeight && payload.size.height != kAppIconHeight)
    {
        CGSize itemSize = CGSizeMake(kAppIconHeight, kAppIconHeight);
        UIGraphicsBeginImageContext(itemSize);
        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        [payload drawInRect:imageRect];
        picture = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    else
    {
        picture = payload;
    }

    self.activeDownload = nil;    
    self.imageConnection = nil;

    [delegate ThumbDidLoad:self.indexPathInTableView Image:picture];
}

以上情侣变化:




1. UIImage * payload = [UIImage imageWithData:self.activeDownload]; 。将此分配更改为自动释放的对象,因为可能会为其分配图片。请注意, if 子句将 picture 分配给自动释放的对象,因此 else 子句也应该,因为有效载荷现在是一个自动释放的对象。


2. UIImage * picture = nil; 而不是 UIImage * picture = [[UIImage alloc] INIT]; 。我这样做是因为图片分配从未使用过,所以nil实际上是有效的,因为它肯定会在中分配,如果 else 条款。


3.现在不需要 [有效负载释放] 有效负载是自动释放的。

Couple changes above:

1. UIImage *payload = [UIImage imageWithData:self.activeDownload];. Changed this assignment to an autoreleased object since picture may be assigned to it. Note that the if clause assigns picture to an autoreleased object, so the else clause should too and now it does since payload is now an autoreleased object.
2. UIImage *picture = nil; instead of UIImage *picture = [[UIImage alloc] init];. I did this since the picture assignment is NEVER used, so nil is actually valid since it will definitely be assigned in either the if or else clause.
3. No need for [payload release] now that payload is autoreleased.

这篇关于如何释放声明为方法并传递给另一个方法的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆