Github每日精选(第45期):异步图像下载器SDWebImage

SDWebImage

SDWebImage 是具有缓存支持的异步图像下载器。该库提供具有缓存支持的异步图像下载器。为方便起见,我们为 UI 元素添加了类别,例如UIImageViewUIButtonMKAnnotationView

github 的地址在这里

在这里插入图片描述

特征
  • UIImageView, UIButton,MKAnnotationView添加 web 图像和缓存管理的类别
  • 一个异步图片下载器
  • 具有自动缓存过期处理的异步内存 + 磁盘映像缓存
  • 背景图像解压缩以避免帧率下降
  • 渐进式图像加载(包括动画图像,如 Web 浏览器中显示的 GIF
  • 缩略图图像解码以节省大图像的 CPU && 内存
  • 可扩展的图像编码器,支持海量图像格式,如 WebP
  • 动态图像的全栈解决方案,在 CPU && 内存之间保持平衡
  • 下载后可立即对图像应用可定制和可组合的转换
  • 可定制的多缓存系统
  • 可定制的多个加载器系统以扩展功能,例如照片库
  • 图像加载指示器
  • 图片加载过渡动画
  • 保证不会多次下载相同的 URL
  • 保证不会一次又一次地重试虚假 URL
  • 保证主线程永远不会被阻塞
  • 现代 Objective-C 和更好的 Swift 支持
支持的图像格式
  • Apple 系统支持的图像格式(JPEGPNGTIFFBMP…),包括GIF / APNG动画图像
  • 来自 iOS 11/macOS 10.13HEIC 格式,包括来自 iOS 13/macOS 10.15 通过 SDWebImageHEICCoder 的动画HEIC。对于较低的固件,使用编码器插件SDWebImageHEIFCoder
  • 来自iOS 14/macOS 11.0的 WebP 格式,通过SDWebImageAWebPCoder。对于较低的固件,请使用编码器插件SDWebImageWebPCoder
  • 支持 BPGAVIF 等新图像格式的可扩展编码器插件。以及PDFSVG等矢量格式。查看图像编码器插件列表中的所有列表

安装指南

在项目中使用 SDWebImage 有四种方法:

  • CocoaPods
  • Carthage
  • Swift Package Manager
  • 手动安装

其中,

使用 CocoaPods 安装

CocoaPodsObjective-C 的依赖管理器,它可以自动化并简化在项目中使用3rd-party库的过程。有关更多详细信息,请参阅入门部分。

platform :ios, '8.0'
pod 'SDWebImage', '~> 5.0'

使用

使用SDAnimatedImageView进行文件的下载,看看下载的速度如如何,我们先用objects 把所有的数据都加入的队列中,在table中进行翻看,看看翻看的速度。

初始化数据:

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"SDWebImage";
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithTitle:@"Clear Cache"
                                                                                style:UIBarButtonItemStylePlain
                                                                               target:self
                                                                               action:@selector(flushCache)];
        
        // HTTP NTLM auth example
        // Add your NTLM image url to the array below and replace the credentials
        [SDWebImageDownloader sharedDownloader].config.username = @"httpwatch";
        [SDWebImageDownloader sharedDownloader].config.password = @"httpwatch01";
        [[SDWebImageDownloader sharedDownloader] setValue:@"SDWebImage Demo" forHTTPHeaderField:@"AppName"];
        [SDWebImageDownloader sharedDownloader].config.executionOrder = SDWebImageDownloaderLIFOExecutionOrder;
        
        self.objects = [NSMutableArray arrayWithObjects:
                    @"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633",     // requires HTTP auth, used to demo the NTLM auth
                    @"http://assets.sbnation.com/assets/2512203/dogflops.gif",
                    @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",
                    @"http://apng.onevcat.com/assets/elephant.png",
                    @"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp",
                    @"http://www.ioncannon.net/wp-content/uploads/2011/06/test9.webp",
                    @"http://littlesvr.ca/apng/images/SteamEngine.webp",
                    @"http://littlesvr.ca/apng/images/world-cup-2014-42.webp",
                    @"https://isparta.github.io/compare-webp/image/gif_webp/webp/2.webp",
                    @"https://nokiatech.github.io/heif/content/images/ski_jump_1440x960.heic",
                    @"https://nokiatech.github.io/heif/content/image_sequences/starfield_animation.heic",
                    @"https://s2.ax1x.com/2019/11/01/KHYIgJ.gif",
                    @"https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/stack_of_photos.pdf",
                    @"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
                    @"http://via.placeholder.com/200x200.jpg",
                    nil];

        for (int i=1; i<25; i++) {
            // From http://r0k.us/graphics/kodak/, 768x512 resolution, 24 bit depth PNG
            [self.objects addObject:[NSString stringWithFormat:@"http://r0k.us/graphics/kodak/kodak/kodim%02d.png", i]];
        }
    }
    return self;
}

下载数据并展现出来:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    static UIImage *placeholderImage = nil;
    if (!placeholderImage) {
        placeholderImage = [UIImage imageNamed:@"placeholder"];
    }
    
    MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.customImageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
        cell.customImageView.sd_imageIndicator = SDWebImageActivityIndicator.grayIndicator;
    }
    
    cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row];
    __weak SDAnimatedImageView *imageView = cell.customImageView;
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]]
                 placeholderImage:placeholderImage
                          options:0
                          context:@{SDWebImageContextImageThumbnailPixelSize : @(CGSizeMake(180, 120))}
                         progress:nil
                        completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        SDWebImageCombinedOperation *operation = [imageView sd_imageLoadOperationForKey:imageView.sd_latestOperationKey];
        SDWebImageDownloadToken *token = operation.loaderOperation;
        if (@available(iOS 10.0, *)) {
            NSURLSessionTaskMetrics *metrics = token.metrics;
            if (metrics) {
                printf("Metrics: %s download in (%f) seconds\n", [imageURL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding], metrics.taskInterval.duration);
            }
        }
    }];
    return cell;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

go2coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值