如何进行多线程以更快地将图像加载到 tableView
Posted
技术标签:
【中文标题】如何进行多线程以更快地将图像加载到 tableView【英文标题】:how to do multi-threading to load the image faster to tableView 【发布时间】:2014-02-10 09:19:55 【问题描述】:-(void)viewDidLoad
[super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.navigationItem.title = @"New Arrivals";
[super viewDidLoad]; idsArray=[[NSMutableArray alloc]init]; namesArray=[[NSMutableArray alloc]init]; imagesArray=[[NSMutableArray alloc]init];
// create the URL we'd like to query
NSURL *myURL = [[NSURL alloc]initWithString:@"http://www.bikrionline.com/nwarvl-json.php"];
// we'll receive raw data so we'll create an NSData Object with it NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
// now we'll parse our data using NSJSONSerialization if ([myData length]>0) id myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil]; NSArray *jsonArray = (NSArray *)myJSON; for (int i=0 ; i<[jsonArray count] ; i++) NSMutableString *urlString = [[NSMutableString alloc]initWithString:@"http://www.bikrionline.com/products/zmphotos/"]; NSString * name = [[[jsonArray objectAtIndex:i]objectForKey:@"prodname"]retain]; [namesArray addObject:name]; [idsArray addObject:[[jsonArray objectAtIndex:i]objectForKey:@"prodid"]]; [urlString appendString:[[jsonArray objectAtIndex:i]objectForKey:@"imageurl"]]; UIImage *imageObj = [self getImageFromUrl:urlString]; urlString = nil; [imagesArray addObject:imageObj];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
CustomCellView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
NSArray *arrNib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellView" owner:self options:nil];
cell = [arrNib objectAtIndex:0];
cell.imgName.text = [namesArray objectAtIndex:indexPath.row];
cell.custImageView.image=[imagesArray objectAtIndex:indexPath.row];
return cell;
【问题讨论】:
【参考方案1】:您可以使用一些 Lib,例如 SDWebImage 或 Afnetworking。库支持延迟加载图像并自动缓存此图像。 所有你需要做的:
[cell.imageView setImageWithURL:[NSURL URLWithString:@"url of image"]
placeholderImage:nil];
【讨论】:
【参考方案2】:这是一个类似延迟加载图像的问题:检查ios lazy-loading of table images 和Loading images on scrollview using lazy loading 为您解答
【讨论】:
以上是关于如何进行多线程以更快地将图像加载到 tableView的主要内容,如果未能解决你的问题,请参考以下文章