SDWebImage---iOS笔记摘录
2018-07-25 本文已影响0人
平安喜乐698
目录
1. 简介
异步图像下载
支持GIF动画
保证相同的url不会下载多次
保证主线程永远不会被阻塞
2. 使用
pod 'SDWebImage'
2.1 UIImageView
#import "UIImageView+WebCache.h"
[imgV sd_setImageWithURL:[NSURL URLWithString:@""]];
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority];
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
// 以上方法都会调用如下方法(这是通常的做法)
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
/*
options:
SDWebImageRetryFailed(失败后重试)
SDWebImageLowPriority(交互时就下载,影响流畅)
SDWebImageCacheMemoryOnly(只进行内存缓存)
SDWebImageProgressiveDownload(渐进式下载)
SDWebImageRefreshCached(刷新缓存,若地址改变图片不变则使用)
SDWebImageContinueInBackground(后台下载)
SDWebImageHandleCookies
SDWebImageAllowInvalidSSLCertificates(允许使用无效SSL)
SDWebImageHighPriority(优先下载)
SDWebImageDelayPlaceholder
SDWebImageTransformAnimatedImage
*/
// 动画
[imgV sd_setAnimationImagesWithURLs:@[[NSURL URLWithString:@""]]];
// 取消当前动画
[imgV sd_cancelCurrentAnimationImagesLoad];

2.2 UIButton
#import "UIButton+WebCache.h"
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRetryFailed];
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
// 以上方法都会调用如下方法(常见封装方式)
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRetryFailed];
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
// 以上方法都会调用如下方法(常见封装方式)
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
2.3 异步下载
#import "SDWebImageDownloader.h"
// 下载图片
SDWebImageDownLoader *downLoader=[SDWebImageDownloader sharedDownloader]downloadImageWithURL:[NSURL URLWithString:@""] options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
};
/*
options:
SDWebImageDownloaderLowPriority 低优先级
SDWebImageDownloaderProgressiveDownload 下载过程中逐步显示
SDWebImageDownloaderUseNSURLCache 使用NSURLCache
SDWebImageDownloaderIgnoreCachedResponse 组合SDWebImageDownloaderUseNSURLCache使用,当图片从缓存中获取时,completed中的image是nil
SDWebImageDownloaderContinueInBackground 支持后台下载
SDWebImageDownloaderHandleCookies 使用Cookies
SDWebImageDownloaderAllowInvalidSSLCertificates 支持不安全的SSL连接(用于测试)
SDWebImageDownloaderHighPriority 高优先级
SDWebImageDownloaderScaleDownLargeImages 缩放大图片
*/
2.4 缓存
#import "SDImageCache.h"
//
SDImageCache *imgCache=[SDImageCache sharedImageCache];
// 缓存
// 缓存图片到沙盒
[imgCache storeImageDataToDisk:[NSData data] forKey:@""];
// 缓存图片到沙盒和内存
[imgCache storeImage:[UIImage imageNamed:@""] forKey:@"" completion:^{
}];
// 缓存图片到内存,是否沙盒
[imgCache storeImage:[UIImage imageNamed:@""] forKey:@"" toDisk:true completion:^{
}];
// 缓存图片到内存,是否沙盒
[imgCache storeImage:[UIImage imageNamed:@""] imageData:nil forKey:@"" toDisk:true completion:^{
}];
// 获取
// 获取缓存图片(先从内存再从沙盒)
UIImage *img=[imgCache imageFromCacheForKey:@"key"];
// 获取缓存图片(从内存)
UIImage *img2=[imgCache imageFromMemoryCacheForKey:@"key"];
// 获取缓存图片(从沙盒)
UIImage *img3=[imgCache imageFromDiskCacheForKey:@"key"];