使用cellForRowAtIndexPath从Parse缓存图像的最佳方法

时间:2014-12-18 18:58:03

标签: ios objective-c uitableview

我通常尝试使用Parse SDK在我的UITableView中加载每个单元格的图像:

PFRelation *relation = [object relationForKey:@"images"];
    PFQuery *relationQuery = [relation query];
    [relationQuery getFirstObjectInBackgroundWithBlock:^(PFObject *obj, NSError *error) {
        if (!error)
        {
            // Adding Image to ImageView
            if ([obj objectForKey:@"image"])
            {


                PFFile *image = (PFFile *)[obj objectForKey:@"image"];
                [image getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
                   if (error)
                    {
                        cell.carPhoto.image = [UIImage imageNamed:@"placeholder.png"];

                    }
                    else
                    {
                        UIImage *carImage = [UIImage imageWithData:data];
                        cell.carPhoto.image = carImage;
                    }
                }];

            }
            else
            {
                cell.carPhoto.image = [UIImage imageNamed:@"placeholder.png"];

            }

        }
    }];

问题在于,它正在使用太多的查询,并且单元格在滚动时有一点延迟,并在实际之前显示上面的图片一小段时间。我试图将我的代码更改为使用SDWebImage:

PFRelation *relation = [object relationForKey:@"images"];
    PFQuery *relationQuery = [relation query];
    [relationQuery getFirstObjectInBackgroundWithBlock:^(PFObject *obj, NSError *error) {
        if (!error)
        {
            // Adding Image to ImageView
            if ([obj objectForKey:@"image"])
            {
                PFFile *image = (PFFile *)[obj objectForKey:@"image"];
                NSString *theUrl = image.url;
                NSLog(@"%@", theUrl);
                [cell.carPhoto setImageWithURL:[NSURL URLWithString:[theUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
                               placeholderImage:nil];


            }
            else
            {
                cell.carPhoto.image = [UIImage imageNamed:@"placeholder.png"];

            }

        }
    }];

但现在我不断得到: - [UIImageView setImageWithURL:placeholderImage:]:无法识别的选择器发送到实例0x7c04ab40。基本上我的最终目标是拥有它,这样每次滚动tableView时都不会查询图像,并且缓存它们。有什么想法吗?

0 个答案:

没有答案