【发布时间】:2014-01-09 06:26:48
【问题描述】:
我能够在 uitableview 上异步获取图像。我正在从 url 获取这些图像。在向上滚动 uitableview 时,这些图像消失了,它们需要时间再次加载,有时它们根本不加载。我不想使用任何 3rd 方库。我不想使用同步方法。请提出任何正确的方法来提高性能。提前感谢帮助。我的代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
}
UIImageView *imgVw=[[UIImageView alloc]initWithFrame:CGRectMake(250, 10, 40, 30)];
[cell.contentView addSubview:imgVw];
Attributes *att = [listOfObjects objectAtIndex:indexPath.row];
strImgUrl=@"http:image url";
strImgName=att.classifiedImg;
if (strImgName == nil) {
UIImage *myImg=[UIImage imageNamed:@"user_circle.png"];
imgVw.image=myImg;
}
else{
strImg=[strImgUrl stringByAppendingString:strImgName];
}
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData *data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:strImg]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *img=[UIImage imageWithData: data];
imgVw.image=img;
});
});
return cell;
}
【问题讨论】:
-
不使用尝试过的party lib意味着你必须写所有你理解和不理解的东西,它不是一个很聪明的方法来找到同一个轮子
标签: ios uitableview uiimageview