使用sdk 4.1。在将缩略图加载到表视图单元的imageview中时,我得到了越来越多的内存占用,随后发生了崩溃(在Instruments中观察到)。此外,即使只有7-8个单元格,滚动也非常不稳定
- (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,当导航到完全不同的视图控制器时,当内存占用持续增长时,是否无论如何都会释放单元持有的图像。只有在选择保存此表视图的视图控制器时,内存才会出现问题。
发布于 2010-09-29 19:41:30
崩溃的原因是你正在释放你不应该释放的NSData对象。
而且滚动表格的速度应该总是很慢,因为每次滚动时,它都将调用cellForRowAtIndexPath方法&使用它将创建一个新图像。
所以试试下面的代码&让我知道它是否能工作
- (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;}
发布于 2010-09-20 21:51:01
我认为问题出在您将CFDataRef转换为NSData这一事实。我猜release方法什么也做不了,因为指针实际上是一个指向应该使用CFRelease函数释放的CFDataRef对象的指针。
尝试:
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];https://stackoverflow.com/questions/3751867
复制相似问题