在我的iPhone应用程序中,我使用自定义的ImageView并使用AsyncImageView类从远程位置加载图像。它可以很好地工作,但是一个问题是,如果我滚动表格,单元格将被取出,并且它会再次尝试从服务器获取图像。所以,从AsyncImageView类加载图像的方法一次又一次地调用,因此增加了内存分配,最终导致应用程序崩溃。在UITableViewCell内存异步图像加载问题
这里是我的代码:
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 300, 40);
CGRect userImageFrame = CGRectMake(5, 7, 36, 36);
UIImageView *userImage;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
[cell setFrame:CellFrame];
userImage = [[UIImageView alloc]init];
userImage.frame = userImageFrame;
userImage.tag = 3;
[cell.contentView addSubview:userImage];
[userImage release];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [self getCellContentView:CellIdentifier];
else
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imageView];
UIImageView *userImage = (UIImageView *)[cell viewWithTag:3];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
NSString *url = [[NSString alloc]initWithFormat:@"%@%@", CommonImageURL,[AllUsersProfileImageArray objectAtIndex:indexPath.row]];
NSURL *imageUrl = [NSURL URLWithString:[url stringByAppendingFormat:@"?%i", rand()]];
[url release];
userImage.image = [UIImage imageNamed:@"defaultPerson.png"];
userImage.imageURL = imageUrl;
return cell;
}
是否有任何可能的方式来解决这个问题?请帮忙。
如果有人需要关于该问题或代码的更多信息,请让我知道。 – Mithuzz
您是在寻找内存泄漏搜索指南还是寻找更好的图像加载解决方案? –
我使用了EGOImageView和现在的AsyncImageView。功能性两者都很好。但问题是内存分配问题。我只是想避免增加的内存分配问题。 – Mithuzz