如何释放在方法中声明并传递给另一个方法的对象?
关于如何在这种情况下释放 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你需要让它自动释放
You need to make it autorelease
我很难理解为什么你的“图片”变量有一个分配初始化。我同意之前使用 autorelease 的答案,但也许更像是:
上面的几个更改:
1.
UIImage *payload = [UIImage imageWithData:self.activeDownload];
。将此分配更改为自动释放的对象,因为可能会将图片分配给它。请注意,if
子句将picture
分配给自动释放的对象,因此else
子句也应该这样做,而且现在确实如此,因为有效负载现在是一个自动释放的对象。2.
UIImage *picture = nil;
而不是UIImage *picture = [[UIImage alloc] init];
。我这样做是因为图片分配从未使用过,所以 nil 实际上是有效的,因为它肯定会在if
或else
子句中分配。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:
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 theif
clause assignspicture
to an autoreleased object, so theelse
clause should too and now it does since payload is now an autoreleased object.2.
UIImage *picture = nil;
instead ofUIImage *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 theif
orelse
clause.3. No need for
[payload release]
now thatpayload
is autoreleased.我认为:
[委托 ThumbDidLoad:self.indexPathInTableView Image:[图片自动释放]];
或者
但我在您的代码中发现两个问题 - 图片 = 有效负载处泄漏;和[有效载荷释放];可以发布图片,也如图所示
I think:
[delegate ThumbDidLoad:self.indexPathInTableView Image:[picture autorelease]];
or
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