UITableView与图像一起慢慢滚动

时间:2013-05-16 10:15:24

标签: iphone objective-c uitableview

我遇到问题我正在加载UITableViewCell images所有向我显示的图片,但问题是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)但尚未成功。

5 个答案:

答案 0 :(得分: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
      });
 });

答案 1 :(得分:0)

您使用SDWebImage从网络服务器加载图片。

答案 2 :(得分:0)

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

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

在这里尝试我的代码,如果它有用则upvote。 How to retrieve images from server asynchronously

答案 3 :(得分:0)

请注意,方法:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]];main thread上运行。

请参阅NSOperatioQueue Samople Code或ASIHTTP库以异步下载图像。

答案 4 :(得分:0)

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

你可以从这里获得图书馆.. https://github.com/nicholjs/SDWebImage-with-loading-progress

相关问题