首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内存泄漏加载缩略图

内存泄漏加载缩略图
EN

Stack Overflow用户
提问于 2010-09-20 21:23:23
回答 2查看 2.2K关注 0票数 3

使用sdk 4.1。在将缩略图加载到表视图单元的imageview中时,我得到了越来越多的内存占用,随后发生了崩溃(在Instruments中观察到)。此外,即使只有7-8个单元格,滚动也非常不稳定

代码语言:javascript
复制
    - (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,当导航到完全不同的视图控制器时,当内存占用持续增长时,是否无论如何都会释放单元持有的图像。只有在选择保存此表视图的视图控制器时,内存才会出现问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-29 19:41:30

崩溃的原因是你正在释放你不应该释放的NSData对象。

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

所以试试下面的代码&让我知道它是否能工作

代码语言:javascript
复制
- (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;

}

票数 1
EN

Stack Overflow用户

发布于 2010-09-20 21:51:01

我认为问题出在您将CFDataRef转换为NSData这一事实。我猜release方法什么也做不了,因为指针实际上是一个指向应该使用CFRelease函数释放的CFDataRef对象的指针。

尝试:

代码语言:javascript
复制
UIImageView* imgView = (UIImageView*)[cell viewWithTag:kImageLabelTag];

CFDataRef contactImageData = ABPersonCopyImageDataWithFormat(personRef,  
                                                    kABPersonImageFormatThumbnail);
UIImage *img = [[UIImage alloc] initWithData:(NSData*)contactImageData];
[imgView setImage:img]; 
CFRelease(contactImageData);
[img release];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3751867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档