内存泄漏加载缩略图 [英] Memory leak loading thumbnail image

查看:45
本文介绍了内存泄漏加载缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SDK 4.1.将缩略图加载到表视图单元格中的imageview时,内存占用量不断增加,随后崩溃(在Instruments中观察到).此外,即使只有7-8个单元格,滚动也非常麻烦

using sdk 4.1. I'm getting growing memory footprint followed by crash (observed in Instruments) when loading a thumbnail image into imageview in table view cell. In addition scrolling is very jerky even with just 7-8 cells

    - (UITableViewCell *)tableView:(UITableView *)tableView  
            cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     static NSString *FavouritesCellIdentifier = @"cellIdentifier";


 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 if (cell == nil) 
 {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:cellIdentifier] autorelease];

    UIImageView* imgView =  [[UIImageView alloc] initWithFrame:CGRectMake(10,
                                                                     16, 64, 64)];
    imgView.tag = kImageLabelTag;
    [cell.contentView addSubview:imgView];
    [imgView release];

   }

   UIImageView* imgView = (UIImageView*)[cell viewWithTag:kImageLabelTag];

    NSData *contactImageData = (NSData*)ABPersonCopyImageDataWithFormat(personRef,  
                                                    kABPersonImageFormatThumbnail);
    UIImage *img = [[UIImage alloc] initWithData:contactImageData];
   [imgView setImage:img]; 
   [contactImageData release];
  [img release];

    return cell;
  }

在viewdidunload中,我设置self.tableview = nil,是否有任何办法释放单元格所保存的图像,因为即使在导航到完全不同的viewcontroller时,内存占用量仍在增长.内存仅在选择包含此TableView的ViewController时拍摄.

In viewdidunload i am setting self.tableview=nil , is there anyway to release the images held by the cell as memory footprint keeps growing even when navigating to totally different viewcontroller. Memory shoots up only when selecting the viewcontroller that holds this tableview.

推荐答案

崩溃的原因是您释放了不应该释放的NSData对象.

The reason for crash is that you're releasing NSData object which you shouldn't.

并且表格的滚动应该总是很慢,因为每次滚动时,它将调用cellForRowAtIndexPath方法&它将创建一个新图像.

And the scrolling of the table should be slow always because with each scroll, it will call cellForRowAtIndexPath method & with it will create a new image.

因此,请尝试以下代码&让我知道它是否有效

So try the below code & let me know whether it works or not

- (UITableViewCell *)tableView:(UITableView *)tableView  
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *FavouritesCellIdentifier = @"cellIdentifier";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:cellIdentifier] autorelease];

    UIImageView* imgView =  [[UIImageView alloc] initWithFrame:CGRectMake(10, 16, 64, 64)];
    imgView.tag = kImageLabelTag;
    [cell.contentView addSubview:imgView];
    [imgView release];

    NSData *contactImageData = (NSData*)ABPersonCopyImageDataWithFormat(personRef, kABPersonImageFormatThumbnail);
    UIImage *img = [[UIImage alloc] initWithData:contactImageData];
    [imgView setImage:img]; 
    [img release];

}

return cell;

}

这篇关于内存泄漏加载缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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