SDWebImage
SDWebImage
是具有缓存支持的异步图像下载器。该库提供具有缓存支持的异步图像下载器。为方便起见,我们为 UI 元素添加了类别,例如UIImageView
、UIButton
、MKAnnotationView
。
github 的地址在这里。
特征
UIImageView
,UIButton
,MKAnnotationView
添加web
图像和缓存管理的类别- 一个异步图片下载器
- 具有自动缓存过期处理的异步内存 + 磁盘映像缓存
- 背景图像解压缩以避免帧率下降
- 渐进式图像加载(包括动画图像,如
Web
浏览器中显示的GIF
) - 缩略图图像解码以节省大图像的
CPU
&& 内存 - 可扩展的图像编码器,支持海量图像格式,如
WebP
- 动态图像的全栈解决方案,在
CPU
&& 内存之间保持平衡 - 下载后可立即对图像应用可定制和可组合的转换
- 可定制的多缓存系统
- 可定制的多个加载器系统以扩展功能,例如照片库
- 图像加载指示器
- 图片加载过渡动画
- 保证不会多次下载相同的
URL
- 保证不会一次又一次地重试虚假
URL
- 保证主线程永远不会被阻塞
- 现代
Objective-C
和更好的Swift
支持
支持的图像格式
Apple
系统支持的图像格式(JPEG
、PNG
、TIFF
、BMP
…),包括GIF / APNG
动画图像- 来自
iOS 11/macOS 10.13
的HEIC
格式,包括来自iOS 13/macOS 10.15
通过SDWebImageHEICCoder
的动画HEIC
。对于较低的固件,使用编码器插件SDWebImageHEIFCoder
- 来自
iOS 14/macOS 11.0
的 WebP 格式,通过SDWebImageAWebPCoder
。对于较低的固件,请使用编码器插件SDWebImageWebPCoder
- 支持
BPG
、AVIF
等新图像格式的可扩展编码器插件。以及PDF
、SVG
等矢量格式。查看图像编码器插件列表中的所有列表
安装指南
在项目中使用 SDWebImage 有四种方法:
- CocoaPods
- Carthage
- Swift Package Manager
- 手动安装
其中,
使用 CocoaPods 安装
CocoaPods
是 Objective-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;
}