如何进行多线程加载图像更快到tableView

时间:2014-02-10 09:19:55

标签: ios

-(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; 
}

2 个答案:

答案 0 :(得分:1)

你可以使用像SDWebImage或Afnetworking这样的Lib。 Libs支持延迟加载图像并自动缓存此图像。 所有你需要做的:

[cell.imageView setImageWithURL:[NSURL URLWithString:@"url of image"]
               placeholderImage:nil];

答案 1 :(得分:-1)

这看起来像是延迟加载图片一样的类似问题:检查iOS lazy-loading of table imagesLoading images on scrollview using lazy loading 为你的答案