使用开源库 SDWebImage 异步下载缓存图片(持续更新)
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:0progress:^(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:0progress:^(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:UITableViewCellStyleDefaultreuseIdentifier: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 异步下载缓存图片(持续更新)相关推荐
- DL:关于深度学习常用数据集中训练好的权重文件(Deeplab v3、MobileNet、InceptionV3、VGG系列、ResNet、Mask R-CNN )下载地址集合(持续更新)
DL:关于深度学习常用数据集中训练好的权重文件(Deeplab v3.MobileNet.InceptionV3.VGG系列.ResNet.Mask R-CNN )下载地址集合(持续更新) 目录 基于 ...
- IOS利用SDWebImage来下载头像图片
pod添加SDWebImage (编辑Podfile文件如下) platform :ios,'7.0' pod 'Masonry', '~> 0.6.1' pod 'MJRefresh', ' ...
- 怎么从开源库github.com 下载
github.com是一个开源库,大量的开源代码等着你去学习,但怎么下载呢?我一直有些迷惑,所以总结一点点放这里. 在开源工程主目录下,在Clone or download 按钮处点击,然后选择 Do ...
- 开源小程序CMS网站,JeeWx-App-CMS 持续更新ing~
JeeWx-App-CMS开源小程序CMS网站,持续更新ing~ JeeWx-App-CMS 是jeewx开发的小程序网站开源项目,基于小程序wepy语言,具备cms网站的基本功能,能够打造简单易用 ...
- 【推荐】[网址]PHP各种开源网站系统、cms系统一览[持续更新]
2019独角兽企业重金招聘Python工程师标准>>> 开源的网站系统很多,今天小编统计整理一下现在流行的各种开源系统.cms推荐,分享给大家参考使用,如果大家有好的资源分享,也请在 ...
- 检测SDWebImage有没有缓存图片 IOS 获取网络图片大小
判断图片是否缓存NSURL *url = [NSURL URLWithString:[model.content objectForKey:@"image"]];//请求网络地址数 ...
- [翻译] 使用开源库 JGDownloadAcceleration 控制下载队列,断点下载,加速下载
JGDownloadAcceleration 本人对原文进行了翻译,凑合看看,使用心得以后补上 https://github.com/JonasGessner/JGDownloadAccelerati ...
- 干货-iOS、mac开源项目及库,以后我也会持续更新。
昨晚在网上看的干货,直接分享给大家了,觉得有用的,直接fork吧. https://github.com/Brances/TimLiu-iOS 转载于:https://www.cnblogs.com/ ...
- 地图下载软件(持续更新)
GISer们在使用GIS时,常用到各种地图资源,除了测绘.购买数据外,还可以从网上找一些免费的地图资源,还有一个方法是使用一些地图下载器软件,这种软件一般都支持下载许多种地图的下载,有影像.地形.PO ...
最新文章
- 学习junit和hamcrest的使用
- 使用百度地图结合GPS进行定位
- Electron中实现拖拽文件进div中通过File对象获取文件的路径和内容
- Android kotlin DataBinding 之 unresolved reference: BR
- 一个countDown在多线程调度下使用不当的分享
- LeetCode【13--罗马数字转整数】LeetCode【14--最长的公共前缀】
- sdk怎么用_PLC不支持OPC UA怎么办?别问了看完你就懂了
- 智慧交通day04-特定目标车辆追踪03:siamese在目标跟踪中的应用-SiamRPN(2017)
- 自然语言处理 —— 2.1 词汇表征
- python声音捕获_在Python中实现实时信号处理如何连续捕获音频?
- 一、 zedboardubuntu 14.04 的前期准备(定期更新)
- 让 ASP.NET AJAX 支持浏览器的 History Navigation - Part 1
- LeaRun低代码平台快速开发工程项目管理软件
- 以下哪些不是Linux操作系统特点,[多选] Linux操作系统具有以下()特点。
- 【Scrum模式语言9】准备就绪的定义(Definition of Ready - DoR )
- 用AI进行工业质检的方案详解!
- 京剧《赤壁》舌战群儒
- ip解析经纬度,基站定位经纬度,用就完事了
- iOS系统越狱研究现状梳理
- 又是暴力裁员?腾讯 7 年老员工一朝被裁,官方回应了...
热门文章
- Cascading Style Sheet 层叠级联样式表
- 解决ssh: no common algorithm for key exchange; client offered: [curve25519-sha256@libssh.org ecdh
- kmeans聚类基本思想
- RK3568平台开发系列讲解(环境篇)编译 Android 固件
- 深度学习中的两阶段目标检测
- 唐骏(一):如何从一名普通程序员到微软中国区总裁
- 为什么下搜酷狗输入法那么快?
- 促销海报怎么做?模板、素材免费分享
- Excel 做相关性分析
- 2021年最全系列蓝牙aoa高精度精准定位基站精准应用介绍深圳核芯物联荣誉出品