如何释放在方法中声明并传递给另一个方法的对象?

发布于 2024-12-04 21:24:59 字数 961 浏览 0 评论 0原文

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

- (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];
}

谢谢您的帮助,

Stephane

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 for helping,

Stephane

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

穿透光 2024-12-11 21:24:59

你需要让它自动释放

UIImage *picture = [[[UIImage alloc] init]autorelease];

You need to make it autorelease

UIImage *picture = [[[UIImage alloc] init]autorelease];
绮烟 2024-12-11 21:24:59

我很难理解为什么你的“图片”变量有一个分配初始化。我同意之前使用 autorelease 的答案,但也许更像是:

- (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 实际上是有效的,因为它肯定会在 ifelse 子句中分配。

3. 由于payload已自动释放,因此不再需要[payload release]

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];
}

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.

胡大本事 2024-12-11 21:24:59

我认为:
[委托 ThumbDidLoad:self.indexPathInTableView Image:[图片自动释放]];

或者

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

但我在您的代码中发现两个问题 - 图片 = 有效负载处泄漏;和[有效载荷释放];可以发布图片,也如图所示

I think:
[delegate ThumbDidLoad:self.indexPathInTableView Image:[picture autorelease]];

or

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

But I see two problems in your code - leaks at picture = payload; and [payload release]; can release image, that is also indicated by the picture

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文