知道为什么下面的行会泄漏内存吗?
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:person.imageURL]];
在 cellForRowAtIndexPath 方法中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell"];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"PersonCell"] autorelease];
}
Person *person = [[Person alloc] initWithUserName:[people objectAtIndex:indexPath.row]];
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:person.imageURL]];
cell.textLabel.text = [people objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[person release];
return cell;
}
这里是person.m的相关方法
- (NSURL*) imageURL
{
return [NSURL URLWithString:[userInfo valueForKey:@"profile_image_url"]];
}
编辑:添加初始化方法:
- (id)initWithUserName:(NSString *)user{
userName = [user copy];
userInfo = [[TwitterHelper fetchInfoForUsername:userName] retain];
updates = [[TwitterHelper fetchTimelineForUsername:userName] retain];
return self;
}
最佳答案
我在这里唯一能想到的可能导致泄漏的是你可能在你的 person 类中保留了 imageURL 而没有在它的 dealloc 方法中释放。所以当你释放 person 时,它会泄露 imageURL 属性。
关于iphone - 为什么这条线会泄漏内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1401278/