在 Objective-C 中的 UIImage 上获取分配计数加一?
我在下面的代码中得到分配计数加一。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MasterViewIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellEditingStyleNone;
UIView* elementView = [ [UIView alloc] initWithFrame:CGRectMake(5,5,312,480)];
elementView.tag = 0;
[cell.contentView addSubview:elementView];
[elementView release];
}
UIView* elementView = [cell.contentView viewWithTag:0];
for(UIView* subView in elementView.subviews)
{
[subView removeFromSuperview];
}
if(indexPath.section==0)
{
if(indexPath.row==0)
{
// i have tried like this but getting exception.
/*
UIImage *cellBackImag=[UIImage imageNamed:@"celltop.png" ];
UIImageView* imaeView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 43)];
imaeView.image=cellBackImag;
[elementView addSubview:imaeView];
[imaeView release];
[cellBackImag release];
*/
// here I am getting uiimage count allocation count
UIImageView* imaeView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"celltop.png" ]];
imaeView.frame=CGRectMake(0, 0, 300, 43);
[elementView addSubview:imaeView];
[imaeView release];
请帮我解决这个问题,谢谢, 马丹·莫汉
I am getting allocation count plus one in the below code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MasterViewIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellEditingStyleNone;
UIView* elementView = [ [UIView alloc] initWithFrame:CGRectMake(5,5,312,480)];
elementView.tag = 0;
[cell.contentView addSubview:elementView];
[elementView release];
}
UIView* elementView = [cell.contentView viewWithTag:0];
for(UIView* subView in elementView.subviews)
{
[subView removeFromSuperview];
}
if(indexPath.section==0)
{
if(indexPath.row==0)
{
// i have tried like this but getting exception.
/*
UIImage *cellBackImag=[UIImage imageNamed:@"celltop.png" ];
UIImageView* imaeView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 43)];
imaeView.image=cellBackImag;
[elementView addSubview:imaeView];
[imaeView release];
[cellBackImag release];
*/
// here I am getting uiimage count allocation count
UIImageView* imaeView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"celltop.png" ]];
imaeView.frame=CGRectMake(0, 0, 300, 43);
[elementView addSubview:imaeView];
[imaeView release];
Please help me out of this, Thank You,
Madan Mohan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,你不应该依赖保留计数,因为当你将对象传递给某个方法时,这个对象可能会发生所有事情,比如有人保留它,再次自动释放它,将它传递给另一个方法,等等。所以值非常不准确。
但在您的情况下,(至少)+1 的保留计数是正确的,因为您希望图像视图在将其作为子视图添加到
elementView
后保持活动状态,因此[ element addSubview:imageView]
保留您的图像视图(将 +1 添加到保留计数),然后再次释放它(保留 alloc init 中的 +1)。注释掉的代码出现异常的原因是您为
cellBackImag
创建了一个自动释放的对象,然后释放了它。方法返回后,在某个时刻自动释放池将再次释放图像,然后它将崩溃。所以基本上只需坚持第二种方法(分配、初始化、添加、释放)并忘记保留计数。如果您担心内存泄漏,您应该看看 Instruments,它是查找各种错误的好工具。
First of all you should not rely on the retain count, because when you pass the object to some method all things might happen with this object, like someone retains it, autoreleases it again, passes it to another method, etc, etc. So the value is highly inaccurate.
But here in your case, the retain count of (at least) +1 is correct, because you want your image view to remain alive after you added it as a subview to
elementView
, so[element addSubview:imageView]
retains your image view (adding +1 to the retain count) and then you release it again (leaving the +1 from alloc init).The reason why you get the exception with the commented out code is that you create an autoreleased object for
cellBackImag
and then you release it. After your method returns, at some point the autorelease pool will release the image again, and there it will crash.So basically just stick with the second approach (alloc, init, add, release) and forget about the retain count. If you're worried about memory leaks, you should take a look at Instruments, which is a great tool for finding all kinds of bugs.