Ios 如何使用缓存异步下载图像并在objective-C的tableviewcell中显示?

Ios 如何使用缓存异步下载图像并在objective-C的tableviewcell中显示?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试使用缓存异步下载图像,并在objective-C的tableviewcell中显示。图像来自URL。我已经使用NSSession编写了API调用,并使用GCD进行了后台调用,但在本例中,图像在滚动时再次下载。我只想下载一次图片 //------------ using GCD --------- if(![imageUrlString isKindOfClass:[NSNull class]]){ dispatch_async(dis

我正在尝试使用缓存异步下载图像,并在objective-C的tableviewcell中显示。图像来自URL。我已经使用NSSession编写了API调用,并使用GCD进行了后台调用,但在本例中,图像在滚动时再次下载。我只想下载一次图片

 //------------ using GCD ---------
        if(![imageUrlString isKindOfClass:[NSNull class]]){

                dispatch_async(dispatch_get_global_queue(0,0), ^{
                  NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlString]];
                    if ( data == nil )
                        return;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        // WARNING: is the cell still using the same data by this point??
                        cell.imageView.image = [UIImage imageWithData: data];
                    });
                });
        }
        //------------ using GCD -----

同样的兑现是唯一的方法吗?如果是,请建议我如何实现这一点。

在开发中,如果我们有合适的工具和库,我们就不需要麻烦。因为我们必须在这方面花费更多的时间

是一个很好的图书馆来满足您的需求。你不必考虑所有的情况。已经为你做了

例如:

imageView.sd_setImage(with: URL(string: "your domain name"), placeholderImage: UIImage(named: "placeholder.png"))
您只需在cellForRowAtIndexPath中写下这一行。:) 这个库中还有很多功能。 快乐的编码。

试试SDWEDMimage

[[SDWebImageManager sharedManager]downloadImageWithURL:[NSURL URLWithString:imageurl] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
    cell.imageView.image =  image; // if you want resize your image and any other operation can do here.
        }];

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageurl]
             placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

为什么不使用SDWebImage?()您是否已阅读此错误的建议-UIImageView sd_setImageWithURL:Placeholder Image::发送到实例的无法识别的选择器打开“构建设置”选项卡,在“链接”部分,找到“其他链接器标志”设置并添加“-ObjC”标志。请尝试此操作,然后在链接二进制库中,添加SDWebImage.a文件。将错误设置为--ld:warning:directory not found for option'-F/Users/LION_2/Desktop/Telstra/build/Debug iphoneos/SDWebImage'ld:library not found for-lSDWebImage clang:error:linker命令失败,退出代码为1(使用-v查看调用)传递为nil。如果不需要占位符图像。获取错误------UIImageView sd_setImageWithURL:placeholder图像:::无法识别的选择器发送到实例------对于代码------[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlString]占位符图像:nil]---您是否将SDWebimage集成到您的项目中?是的..使用POD。。pod'SDWebImage','~>3.6',然后导入此文件#导入UIImageView+WebCache.h和#导入“SDWebImageManager.h”