天天看点

使用开源库 SDWebImage 异步下载缓存图片(持续更新)

使用开源库 SDWebImage 异步下载缓存图片(持续更新)
使用开源库 SDWebImage 异步下载缓存图片(持续更新)

asynchronous image downloader with cache support with an uiimageview category

uiimageview的类目,支持异步图片下载,支持缓存机制

this library provides a category for uiimageview with support for remote images coming from the web.

这个库给uiimageview提供类目,支持远程下载图片(从网络上)

it provides:

an uiimageview category adding web image and cache management to the cocoa touch framework

an asynchronous image downloader

an asynchronous memory + disk image caching with automatic cache expiration handling

animated gif support

webp format support

a background image decompression

a guarantee that the same url won't be downloaded several times

a guarantee that bogus urls won't be retried again and again

a guarantee that main thread will never be blocked

performances!

use gcd and arc

arm64 support

一个uiimageview的类目,给 cocoa touch 框架添加了异步下载远程图片以及管理图片缓存的功能

一个图片的异步下载器

一个内存 + 磁盘的缓存机制,并自动管理

gif动画支持

webp格式支持

后台解压图片

确保同样地 url 不会重复的下载多次

确保无效的 url 不会重复的链接

确保主线程永远不会阻塞

效果拔群!

使用gcd以及要求arc

支持64位系统

以下进行sdwebimage使用的教程解说.

2. 头文件较多,请新建一个 sdwebimage.h 的头文件,写以下代码并包含所有头文件,添加到.pch文件中

-------------------------------------------------------------------------------

//mkannotationview地图的注解view缓存

#import "mkannotationview+webcache.h"

//判断nsdata是否什么类型的图片(例如:jpg,png,gif)

#import "nsdata+imagecontenttype.h"

//是sdwebimage包的一部分

#import "sdimagecache.h"      //缓存相关

#import "sdwebimagecompat.h"  //组件相关

#import "sdwebimagedecoder.h" //解码相关

//图片下载以及下载管理器

#import "sdwebimagedownloader.h"

#import "sdwebimagedownloaderoperation.h"

//管理以及操作

#import "sdwebimagemanager.h"

#import "sdwebimageoperation.h"

//uibutton类目

#import "uibutton+webcache.h"

//gif类目

#import "uiimage+gif.h"

//图片其他类目

#import "uiimage+multiformat.h"

#import "uiimage+webp.h"

#import "uiimageview+webcache.h"

3. 正式开始讲解怎么使用

独立的下载图片的功能(没有缓存机制)

分析:此方法为单例模式,看其源码

+ (sdwebimagedownloader *)shareddownloader {

    static dispatch_once_t once;

    static id instance;

    dispatch_once(&once, ^{

        instance = [self new];

    });

    return instance;

}

- (id)init {

    if ((self = [super init])) {

        _executionorder = sdwebimagedownloaderfifoexecutionorder;

        _downloadqueue = [nsoperationqueue new];

        _downloadqueue.maxconcurrentoperationcount = 2;

        _urlcallbacks = [nsmutabledictionary new];

        _httpheaders = [nsmutabledictionary dictionarywithobject:@"image/webp,image/*;q=0.8" forkey:@"accept"];

        _barrierqueue = dispatch_queue_create("com.hackemist.sdwebimagedownloaderbarrierqueue", dispatch_queue_concurrent);

        _downloadtimeout = 15.0;

    }

    return self;

typedef ns_enum(nsinteger, sdwebimagedownloaderexecutionorder) {

    /**

     * default value. all download operations will execute in queue style (first-in-first-out). 默认值.所有的下载操作将会进入串行线程池fifo

     */

    sdwebimagedownloaderfifoexecutionorder,

     * all download operations will execute in stack style (last-in-first-out).

    sdwebimagedownloaderlifoexecutionorder

};

如果仅仅看上面的部分,知道下载单例由串行线程池管理着,按照队列执行,一次最多能执行两个,但我在实际测试过程中发现,并不像描述的那样子......,好吧,就当做是并发执行的了(此处疑问有时间再解决)

独立的下载图片的功能(有缓存机制)

清除缓存文件

判断本地缓存中是否存在网络中的图片

获取缓存图片张数

获取所有缓存图片的总大小

直接从缓存中提取图片

直接删除缓存中得图片

在uitableview中使用

-未完待续-