iOS之SDWEBIMAGE的使用

2024-05-31 10:48
文章标签 使用 ios sdwebimage

本文主要是介绍iOS之SDWEBIMAGE的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考:http://blog.csdn.net/benyoulai5/article/details/50462586

http://www.jianshu.com/p/7a2aab8553fe

https://www.jianshu.com/p/7dea5b081d24

让sdwebimage不缓存图片,每次都重新加载url:https://blog.csdn.net/feiyuyuan_9257/article/details/77717343

 [imageview sd_setImageWithURL:url placeholderImage:image options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {}];
viewdidload 或init方法里面加入-(void)sdWEbDownloaderRegister{SDWebImageDownloader *imgDownloader = SDWebImageManager.sharedManager.imageDownloader;imgDownloader.headersFilter  = ^NSDictionary *(NSURL *url, NSDictionary *headers) {NSFileManager *fm = [[NSFileManager alloc] init];NSString *imgKey = [SDWebImageManager.sharedManager cacheKeyForURL:url];NSString *imgPath = [SDWebImageManager.sharedManager.imageCache defaultCachePathForKey:imgKey];NSDictionary *fileAttr = [fm attributesOfItemAtPath:imgPath error:nil];NSMutableDictionary *mutableHeaders = [headers mutableCopy];NSDate *lastModifiedDate = nil;if (fileAttr.count > 0) {if (fileAttr.count > 0) {lastModifiedDate = (NSDate *)fileAttr[NSFileModificationDate];}}NSDateFormatter *formatter = [[NSDateFormatter alloc] init];formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];formatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss z";NSString *lastModifiedStr = [formatter stringFromDate:lastModifiedDate];lastModifiedStr = lastModifiedStr.length > 0 ? lastModifiedStr : @"";[mutableHeaders setValue:lastModifiedStr forKey:@"If-Modified-Since"];return mutableHeaders;};
}

SDWebImage 概论

1.提供了一个UIImageView的category用来加载网络图片并且对网络图片的缓存进行管理
2.采用异步方式来下载网络图片
3.采用异步方式,使用memory+disk来缓存网络图片,自动管理缓存。
4.支持GIF动画
5.支持WebP格式
6.同一个URL的网络图片不会被重复下载
7.失效的URL不会被无限重试
8.耗时操作都在子线程,确保不会阻塞主线程
9.使用GCD和ARC
10.支持Arm64

SDWebImage 使用

1.使用IImageView+WebCache category来加载UITableView中cell的图片

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

2.使用Blocks,采用这个方案可以在网络图片加载过程中得知图片的下载进度和图片加载成功与否

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"] completed:^(UIImage image, NSError error, SDImageCacheType cacheType, NSURL *imageURL) { ... completion code here ... }];

3.使用SDWebImageManager,SDWebImageManager为UIImageView+WebCache category的实现提供接口。

SDWebImageManager manager = [SDWebImageManager sharedManager] ;[manager downloadImageWithURL:imageURL options:0 progress:^(NSInteger   receivedSize, NSInteger expectedSize) { // progression tracking code }   completed:^(UIImage image, NSError error, SDImageCacheType cacheType,   BOOL finished, NSURL imageURL) { if (image) { // do something with image } }];

4.加载图片还有使用SDWebImageDownloader和SDImageCache方式,但那个并不是我们经常用到的。基本上面所讲的3个方法都能满足需求。

SDWebImage 流程

几个常用接口:

  1. 获取SDWebImage的磁盘缓存大小,在项目中有时候会需要统计应用的磁盘缓存内容大小,那么获取图片的缓存大小就是使用这个接口来实现

    [SDImageCache sharedImageCache] getSize];
  2. 清理内存缓存,清理内存中缓存的图片资源,释放内存资源。

    [[SDImageCache sharedImageCache] clearMemory];
  3. 有了清理内存缓存,自然也有清理磁盘缓存的接口

    [[SDImageCache sharedImageCache] clearDisk];

     

//////////////////////////////

 

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "SDWebImageManager.h"
#import "SDWebImageDownloader.h"
#import "UIImage+GIF.h"
#import "NSData+ImageContentType.h"@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{[self download];
}//1.下载图片且需要获取下载进度
/***    1:当需要获取下载进度的时候可以使用此方法,其中optiobs如果什么都不做,可以传参数0,optiobs是一个位移枚举,可以通过按位或 | 来并列添加参数 2:SDImageCacheType缓存的类型:此方法默认做了内存缓存,与磁盘缓存,第一次是直接下载,然后点击的时候是从内存缓存中读取,当内存缓存不存在的时候,再从磁盘缓存读取**/
//内存缓存&磁盘缓存
-(void)download
{[self.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.600_0.jpeg"] placeholderImage:[UIImage imageNamed:@"Snip20160221_306"] options:SDWebImageCacheMemoryOnly | SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {switch (cacheType) {case SDImageCacheTypeNone:NSLog(@"直接下载");break;case SDImageCacheTypeDisk:NSLog(@"磁盘缓存");break;case SDImageCacheTypeMemory:NSLog(@"内存缓存");break;default:break;}}];NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]);}//2.只需要简单获得一张图片,不设置/*** 1:默认SDImageCacheType是内存缓存&磁盘缓存。如果只是简单下载一张图片,就用如下的方法:[SDWebImageManager sharedManager] downloadImageWithURL**/-(void)download2
{[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.600_0.jpeg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {NSLog(@"%f",1.0 * receivedSize / expectedSize);} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {//得到图片self.imageView.image = image;}];
}//3.不需要任何的缓存处理
/*** 没有做任何缓存处理**/-(void)download3
{//data:图片的二进制数据[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.600_0.jpeg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {[[NSOperationQueue mainQueue]addOperationWithBlock:^{self.imageView.image = image;}];}];
}//4.播放Gif图片
/***播放Gif图片*/
-(void)gif
{NSLog(@"%s",__func__);//self.imageView.image = [UIImage imageNamed:@"39e805d5ad6eddc4f80259d23bdbb6fd536633ca"];UIImage *image = [UIImage sd_animatedGIFNamed:@"39e805d5ad6eddc4f80259d23bdbb6fd536633ca"];self.imageView.image = image;
}-(void)type
{NSData *imageData = [NSData dataWithContentsOfFile:@"/Users/xiaomage/Desktop/Snip20160221_306.png"];NSString *typeStr = [NSData sd_contentTypeForImageData:imageData];NSLog(@"%@",typeStr);
}
@end

复制代码

二:当内存产生警告的时候,清除缓存

复制代码

#import "AppDelegate.h"
#import "SDWebImageManager.h"@interface AppDelegate ()@end@implementation AppDelegate-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{//1.清空缓存//clear:直接删除缓存目录下面的文件,然后重新创建空的缓存文件//clean:清除过期缓存,计算当前缓存的大小,和设置的最大缓存数量比较,如果超出那么会继续删除(按照文件了创建的先后顺序)//过期时间:7天[[SDWebImageManager sharedManager].imageCache clearMemory];//2.取消当前所有的操作[[SDWebImageManager sharedManager] cancelAll];//3.最大并发数量 == 6//4.缓存文件的保存名称如何处理? 拿到图片的URL路径,对该路径进行MD5加密//5.该框架内部对内存警告的处理方式? 内部通过监听通知的方式请你缓存//6.该框架进行缓存处理的方式:可变字典--->NSCache//7.如何判断图片的类型: 在判断图片类型的时候,只匹配第一个字节//8.队列中任务的处理方式:FIFO//9.如何下载图片的? 发送网络请求下载图片,NSURLConnection//10.请求超时的时间 15秒//[NSData dataWithContentsOfURL:<#(nonnull NSURL *)#>]
}
@end

复制代码

三:SDWebImage的结构:最顶层的父类是SDWebImageManager,其下有两个子类如图所示

 

 

四:知识点总结:01 设置imageView的图片[cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"placehoder"]]; 02 设置图片并计算下载进度 //下载并设置图片 /* 第一个参数:要下载图片的url地址 第二个参数:设置该imageView的占位图片 第三个参数:传一个枚举值,告诉程序你下载图片的策略是什么 第一个block块:获取当前图片数据的下载进度 receivedSize:已经下载完成的数据大小 expectedSize:该文件的数据总大小 第二个block块:当图片下载完成之后执行该block中的代码 image:下载得到的图片数据 error:下载出现的错误信息 SDImageCacheType:图片的缓存策略(不缓存,内存缓存,沙盒缓存) imageURL:下载的图片的url地址 */ [cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"placehoder"] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) { //计算当前图片的下载进度 NSLog(@"%.2f",1.0 *receivedSize / expectedSize); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { }]; 03 系统级内存警告如何处理(面试) //取消当前正在进行的所有下载操作 [[SDWebImageManager sharedManager] cancelAll]; //清除缓存数据(面试) //cleanDisk:删除过期的文件数据,计算当前未过期的已经下载的文件数据的大小,如果发现该数据大小大于我们设置的最大缓存数据大小,那么程序内部会按照按文件数据缓存的时间从远到近删除,知道小于最大缓存数据为止。 //clearMemory:直接删除文件,重新创建新的文件夹 //[[SDWebImageManager sharedManager].imageCache cleanDisk]; [[SDWebImageManager sharedManager].imageCache clearMemory]; 04 SDWebImage默认的缓存时间是1周 05 如何播放gif图片 /* 5-1 把用户传入的gif图片->NSData 5-2 根据该Data创建一个图片数据源(NSData->CFImageSourceRef) 5-3 计算该数据源中一共有多少帧,把每一帧数据取出来放到图片数组中 5-4 根据得到的数组+计算的动画时间-》可动画的image [UIImage animatedImageWithImages:images duration:duration]; */ 06 如何判断当前图片类型,只判断图片二进制数据的第一个字节 + (NSString *)sd_contentTypeForImageData:(NSData *)data; 07 内部如何进行缓存处理?使用了NSCache类,使用和NSDictionary类似 08 沙盒缓存图片的命名方式为对该图片的URL进行MD5加密 echo -n "url" |MD5 09 当接收到内存警告之后,内部会自动清理内存缓存 10 图片的下载顺序,默认是先进先出的:FIFO原则

 

********新版的sdwebimage找不到  sd_animatedGIFWithData方法,需要自己查创建,

 UIImage * image = [self sd_animatedGIFWithData:data];//新版的sdwebimage没有下面的方法
-(UIImage *)sd_animatedGIFWithData:(NSData *)data {if (!data) {return nil;}CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);size_t count = CGImageSourceGetCount(source);UIImage *animatedImage;if (count <= 1) {animatedImage = [[UIImage alloc] initWithData:data];}else {NSMutableArray *images = [NSMutableArray array];NSTimeInterval duration = 0.0f;for (size_t i = 0; i < count; i++) {CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);duration += [self sd_frameDurationAtIndex:i source:source];[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];CGImageRelease(image);}if (!duration) {duration = (1.0f / 10.0f) * count;}animatedImage = [UIImage animatedImageWithImages:images duration:duration];}CFRelease(source);return animatedImage;
}-(float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {float frameDuration = 0.1f;CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];if (delayTimeUnclampedProp) {frameDuration = [delayTimeUnclampedProp floatValue];}else {NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];if (delayTimeProp) {frameDuration = [delayTimeProp floatValue];}}if (frameDuration < 0.011f) {frameDuration = 0.100f;}CFRelease(cfFrameProperties);return frameDuration;
}

 

 

这篇关于iOS之SDWEBIMAGE的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1017768

相关文章

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

C#中lock关键字的使用小结

《C#中lock关键字的使用小结》在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时,其他线程无法访问同一实例的该代码块,下面就来介绍一下lock关键字的使用... 目录使用方式工作原理注意事项示例代码为什么不能lock值类型在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时