source  https://github.com/rs/SDWebImage

APIdoc  http://hackemist.com/SDWebImage/doc

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使用的教程解说.

1. 从地址 https://github.com/rs/SDWebImage 下载源码,将源码包中得 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. 正式开始讲解怎么使用

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

    NSString *oneImageURL =
@"http://wallpapers.wallbase.cc/rozne/wallpaper-573934.jpg"; [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:oneImageURL]
options: progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
//此处为下载进度
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
{
//下载完成后进入这里执行
}];

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

+ (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
};

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

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

    NSString *oneImageURL =
@"http://pic.cnitblog.com/avatar/607542/20140226182241.png"; [[SDWebImageManager sharedManager] downloadWithURL:[NSURL URLWithString:oneImageURL]
options: progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
//此处为下载进度
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
//下载完成后进入这里执行
}];

清除缓存文件

    [[SDImageCache sharedImageCache] clearDisk];

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

    NSString *imageNetURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png";
[[SDImageCache sharedImageCache] diskImageExistsWithKey:imageNetURL];

获取缓存图片张数

    [[SDImageCache sharedImageCache] getDiskCount];

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

    [[SDImageCache sharedImageCache] getSize];

直接从缓存中提取图片

    NSString *imageNetURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png";
[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imageNetURL];

直接删除缓存中得图片

    NSString *imageNetURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png";
[[SDImageCache sharedImageCache] removeImageForKey:imageNetURL];

在UITableView中使用

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"Y.X."; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
} // Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; cell.textLabel.text = @"Y.X.";
return cell;
}

-未完待续-

使用开源库 SDWebImage 异步下载缓存图片(持续更新)的更多相关文章

  1. android开发步步为营之67:使用android开源项目android-async-http异步下载文件

    android-async-http项目地址 https://github.com/loopj/android-async-http.android-async-http顾名思义是异步的http请求, ...

  2. Android开源库--Universal Image Loader通用图片加载器

    如果说我比别人看得更远些,那是因为我站在了巨人的肩上.   github地址:https://github.com/nostra13/Android-Universal-Image-Loader 介绍 ...

  3. [翻译] 使用开源库 JGDownloadAcceleration 控制下载队列,断点下载,加速下载

    JGDownloadAcceleration 本人对原文进行了翻译,凑合看看,使用心得以后补上 https://github.com/JonasGessner/JGDownloadAccelerati ...

  4. 好用的开源库(二)——uCrop 图片裁剪

    最近想要实现图片裁剪的功能,在Github上找到了这个uCrop,star的人挺多的,便是决定入坑,结果长达一个小时的看资料+摸索,终于是在项目中实现了图片裁剪的功能,今天便是来介绍一下uCrop的使 ...

  5. 干货-iOS、mac开源项目及库,以后我也会持续更新。

    昨晚在网上看的干货,直接分享给大家了,觉得有用的,直接fork吧. https://github.com/Brances/TimLiu-iOS

  6. Android开源项目发现----其他特殊效果篇(持续更新)

    1. Crouton 丰富样式的Toast 允许alert.comfirm.info样式及点击消失样式,允许设置Toast显示时间,允许自定义View. 项目地址:https://github.com ...

  7. MSSQL部分补丁的列表及下载地址(持续更新)

    整理了MSSQL部分补丁的列表及下载地址(截至2016-11-18),供参考下. Edition Version Date Published Download Link SQL Server 201 ...

  8. Windows 10原版ISO下载地址(持续更新)

    Windows 10本质上,它们与 Win7.XP 时代的 SP1.SP2.SP3 这样的大型更新版是一样的,只不过微软很蛋疼地为它们起上一个难记地要死的名字,仅此而已.如果你把“一周年更新”看作 S ...

  9. Cadence物理库 LEF 文件语法学习【持续更新】

    我是 雪天鱼,一名FPGA爱好者,研究方向是FPGA架构探索. 关注公众号,拉你进"IC设计交流群". @ 目录 一.LEF简介 1.1 通用规则 1.2 管理 LEF 文件 二. ...

随机推荐

  1. (使用通过混淆+自己第三方保留成功混淆)AndroidStudio 混淆打包

    原文:https://blog.csdn.net/mazhidong/article/details/64820838 AndroidStudio中的项目可以用compile的形式引入github上的 ...

  2. markdown 语法规则

    markdown 语法规则 参考Markdown--入门指南 也可以参考这篇 Markdown: Basics (快速入门) 一级标题 二级标题 三级标题 列表 无序 1 2 3 有序 1 2 3 需 ...

  3. jquery实现checkbox的单选和全选

    一.思路 全选:判断“全选”checkbox的状态,如果选中则把tbody下所有的checkbox选中,反之 单选:主要是判断有没有全选,如果不是选中状态就把全选的checkbox状态设置为false ...

  4. python3与mysql交互:pymysql

    python3与mysql交互 1.安装pymysql模块 pip3 install pymysql3 2.pymysql的简单使用: # /usr/bin/env python3 import py ...

  5. Ionic Js十三:平台

    $ionicPlatform $ionicPlatform 用来检测当前的平台,以及诸如在PhoneGap/Cordova中覆盖Android后退按钮.  

  6. JAVAEE——宜立方商城05:前台系统搭建、首页展示、Cms系统的实现

    1. 学习计划 1.前台系统搭建 2.商城首页展示 3.Cms系统的实现 a) 内容分类管理 b) 内容管理 4.前台内容动态展示 2. 商城首页展示 系统架构: 页面位置: 2.1. 工程搭建 可以 ...

  7. [leetcode greedy]134. Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  8. 1003 Emergency (25)(25 point(s))

    problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...

  9. OpenStack计费项目Cloudkitty系列详解(一)

    云计算是一种按需付费的服务模式,虽然OpenStack前期在计量方面走了些“弯路”,但现在的ceilometer.gnocchi.aodh.panko项目的稳步并进算是让其峰回路转.然而,目前来看Op ...

  10. android studio 添加有趣的注释模板 佛祖保佑无bug等

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...