如何在UITableview中改善加载图像?

时间:2016-10-01 02:52:21

标签: ios objective-c uitableview uiimageview

我在表视图中加载imageview时遇到问题。加载速度很慢。我在表格视图中使用了很多部分。有我的代码:

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 TrangChuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellTrangchu" forIndexPath:indexPath];

theGame *thegameObj;
if([indexPath section] == 0){

    thegameObj = [theZingArray objectAtIndex:indexPath.row];

}else if([indexPath section] == 1){

    thegameObj = [theBitArray objectAtIndex:indexPath.row];

}else if([indexPath section] == 2){

    thegameObj = [theMobayArray objectAtIndex:indexPath.row];

}else if([indexPath section] == 3){

    thegameObj = [theGateArray objectAtIndex:indexPath.row];

}else if([indexPath section] == 4){

    thegameObj = [theVcoinArray objectAtIndex:indexPath.row];

}else if([indexPath section] == 5){

    thegameObj = [theGarenaArray objectAtIndex:indexPath.row];

}else if([indexPath section] == 6){

    thegameObj = [theOncashArray objectAtIndex:indexPath.row];

}else if([indexPath section] == 7){

    thegameObj = [theMobiphoneArray objectAtIndex:indexPath.row];

}else if([indexPath section] == 8){

    thegameObj = [theVinaphoneArray objectAtIndex:indexPath.row];

}else if([indexPath section] == 9){

    thegameObj = [theViettelArray objectAtIndex:indexPath.row];

}
NSURL *urlImage = [NSURL URLWithString:thegameObj.image];
NSData *imageData = [NSData dataWithContentsOfURL:urlImage];
cell.txtQuantity.text=@"1";
UIImage *image= [UIImage imageWithData:imageData];
cell.imageView.layer.cornerRadius=5;
cell.imageView.layer.masksToBounds=YES;
cell.imageView.image = image;
cell.labelName.text = thegameObj.objectName;

if([[AppDelegate appDelegate]checkIP])
{
    [cell.txtQuantity setBackgroundColor:[UIColor whiteColor]];
    [cell.txtQuantity.layer setBorderColor:[UIColor grayColor].CGColor];
    [cell.txtQuantity.layer setBorderWidth:1.0];
    [cell.txtQuantity.layer setCornerRadius:5];
    cell.labelPrice.text = [NSString stringWithFormat:@"%@ (%@)", [NSNumberFormatter localizedStringFromNumber:thegameObj.price numberStyle:NSNumberFormatterDecimalStyle], loadCurrency];

}
else
{
    [cell.lbdetail setTitle:@"detail" forState:UIControlStateNormal];
    cell.txtQuantity.hidden=YES;
    cell.labelPrice.hidden=YES;
    cell.lbgia.hidden=YES;
    cell.lbsl.hidden=YES;
    cell.lbdetail.hidden=YES;

}

cell.objArray = thegameObj;
cell.currency = loadCurrency;
/*
 [cell.addToCartButton addTarget:self action:@selector(addToCart:) forControlEvents:UIControlEventTouchUpInside];
 cell.addToCartButton.tag = [NSString stringWithFormat:@"%d_%d", (NSInteger)[indexPath section], (NSInteger)[indexPath row]];
 */
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


return cell;
}

请帮助我在UItableview中使用大量部分来更快地加载图像。谢谢你的任何解决方案!

3 个答案:

答案 0 :(得分:2)

有图像缓存的概念,图像视图使用它在背景中异步加载图像。请参阅以下链接 https://github.com/nicklockwood/AsyncImageView

答案 1 :(得分:2)

有一点可以帮助的是一个匹配分段表模型的数据结构,这是一个数组数组。你几乎就在那里,因为你有所有的子阵列。像这样构建一个(当构建所有子数组时,而不是在cellForRow中):

curl -H "Authorization: Bearer <my_personal_access_token>" "https://app.asana.com/api/1.0/tags/<tag_id>/tasks?opt_fields=name,tags,tags.name"

这会让你将大条件扁平化为一行:

NSArray *model = @[ theZingArray, theBitArray, /*... and so on */];

您可以在其他地方使用它,例如thegameObj = model[indexPath.section][indexPath.row];

numberRowsInSection

稍微困难的问题是让这些图像加载异步并缓存。讨论了完全原生的方法here和许多others

将此想法应用于您的代码,这是一个返回缓存图像或提取,缓存和返回的方法...

NSArray *sectionArray = model[indexPath.section];
return sectionArray.count;

现在您的简化// declare this and BE SURE TO INITIALIZE IT @property(strong,nonatomic) NSMutableDictionary *imageCache; // return an image found at the url string, if one is cached return it, otherwise, // fetch it, cache it and return it - (void)imageAtURLString:(NSString *)urlString completion:(void(^)(UIImage *, BOOL))completion { if (self.imageCache[urlString]) { // if it's cached completion(self.imageCache[urlString], YES); } else { // fetch and cahce NSURL *url = [NSURL URLWithString:urlString]; NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (data) { UIImage *image = [UIImage imageWithData:data]; if (image) { self.imageCache[urlString] = image; dispatch_async(dispatch_get_main_queue(), ^{ completion(image, NO); }); } } }]; [task resume]; } } 可能如下所示:

cellForRowAtIndexPath

这里的想法是你有一个缓存的图像,在这种情况下,只需将单元格的imageView图像设置为,或者你必须获取一个。在获取的情况下,不确定在获取期间该行是否被滚动。重新加载当前行以使其使用(现在缓存的)图像进行更新。

答案 2 :(得分:0)

使用以下真棒库在后台下载图像。

https://github.com/rs/SDWebImage

UIImageView * thumbnail = [[UIImageView alloc] init];
NSURL *urlToPicture1 = [NSURL URLWithString:currentMarker.thumbnail];

[thumbnail sd_setImageWithURL:urlToPicture1 placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageProgressiveDownload completed:^(UIImage * image, NSError * error,SDImageCacheType cachedType, NSURL * imageURL){
    if(image){
        [thumbnail setImage:image];
        NSLog(@"Complete = %d, error = %@",cachedType, error);
    } 
}];
相关问题