0

我有问题我正在加载显示给我UITableViewCellimages所有图像但问题UTableView scroll很慢这是代码

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

    if(cell == nil)
    {
        cell =[[customSearchesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DefaultCell"];        
    }    

    searchobjectval =[self.arrSearchResult objectAtIndex:indexPath.row];
    cell.location.text =searchobjectval.location;
    cell.title.text = searchobjectval.title;
    cell.cost.text = searchobjectval.roomCost;
    cell.desc.text =searchobjectval.description;

    [tableView setSeparatorColor:[UIColor blueColor]];

    UIView *myView = [[UIView alloc] init];
    if (indexPath.row % 2)
    {
        UIColor* clr = [UIColor colorWithRed:0.4f green:0.6f blue:0.8f alpha:1];
        myView.backgroundColor = clr;
    }
    else
    {
        myView.backgroundColor = [UIColor whiteColor];
    }
    cell.backgroundView = myView;

    NSLog(@" search object image %@",searchobjectval.imgLink);

    UIImage *ret = [self imageNamed:[NSString stringWithFormat: @"http://192.95.76.78/bedspace/new_arrivals_img/%@",searchobjectval.idVal ] cache:YES andsearchObj:searchobjectval];

    cell.imgVw.image = ret;

    return cell;
}

    // images caching in dictionary
    - (UIImage*)imageNamed:(NSString*)imageNamed cache:(BOOL)cache andsearchObj:(clsSearch *)searchObj
    {
         UIImage* retImage = [staticImageDictionary objectForKey:imageNamed];
        @try
        {
            if (retImage == nil)
            {
                retImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]];
                if (cache)
                {
                    if (staticImageDictionary == nil)
                        staticImageDictionary = [NSMutableDictionary new];

                    [staticImageDictionary setObject:retImage forKey:imageNamed];

                }               
            }
        }
        @catch (NSException *exception)
        {
            NSLog(@"exception %@",exception);
        }
        @finally {
            NSLog(@"finally block execute here");
        }

        return retImage;
    }

请告诉我如何解决此问题,我已将代码粘贴到其中if (cell == nil)但尚未成功。

4

5 回答 5

0

您可以使用 SDWebImage 库,它不仅可以异步加载图像,还可以使您的 tableView 滚动非常好和快速。

你可以从这里获取库.. https://github.com/nicholjs/SDWebImage-with-loading-progress

于 2013-05-16T10:25:27.467 回答
0

您用来SDWebImage从网络服务器加载图像。

于 2013-05-16T10:21:14.533 回答
0

retImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]];

对于出现的每个单元格,此行开始从服务器获取数据(图像)。随着您开始滚动UITable,请求将转到服务器以获取图像,因此您应该使用NSThread.

在这里尝试我的代码,如果有用,请点赞。 如何从服务器异步检索图像

于 2013-05-16T10:21:26.290 回答
0

请注意,方法:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]];main thread.

请参阅 NSOperationQueue Samople 代码或 ASIHTTP 库以异步下载图像。

于 2013-05-16T10:22:16.107 回答
0

问题是将图像同步加载到tablecell中。使其异步并且可以正常工作

用这个

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
 dispatch_async(queue, ^{
       NSString *strURL = url here;
       NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
       UIImage *image = nil;
       if(data)
        image = [UIImage imageWithData:data];
      dispatch_sync(dispatch_get_main_queue(), ^{
           //now use image in image View  or anywhere according to your requirement.
           if(image)
             yourImgView = image
      });
 });
于 2013-05-16T10:20:18.870 回答