SDWebImage (5.1.0)UIImageView+WebCache.h(五)

所有方法最终会进入这个最长的方法

- (void)testUIImageViewWebCache{
    
    //https://sdwebimage.github.io/Categories/UIImageView(WebCache).html
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.backgroundColor = [UIColor greenColor];
    imageView.frame = self.view.bounds;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:imageView];
    
    imageView.sd_imageProgress = [[NSProgress alloc]initWithParent:nil userInfo:nil];
    imageView.sd_imageIndicator = [SDWebImageActivityIndicator grayLargeIndicator];
    imageView.sd_imageIndicator = [SDWebImageProgressIndicator defaultIndicator];
    imageView.sd_imageTransition = [SDWebImageTransition curlUpTransition];
    
    NSString *url = @"http://img4.cache.netease.com/photo/0001/2010-04-17/64EFS71V05RQ0001.jpg";
    NSString *gifUrl = @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif";
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:gifUrl] placeholderImage:[UIImage imageNamed:@"placeHolder.jpeg"] options:SDWebImageRefreshCached context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        
        NSLog(@"receivedSize%ld,expectedSize%ld",(long)receivedSize,(long)expectedSize);
        
        
    } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
       
        NSLog(@"image%@,error%@,cacheType%ld",image,error,(long)cacheType);
        
    }];
}

最后进入这里sd_internalSetImageWithURL这个方法,这个方法是UIView+WebCache.h里定义的

- (void)sd_setImageWithURL:(nullable NSURL *)url
          placeholderImage:(nullable UIImage *)placeholder
                   options:(SDWebImageOptions)options
                   context:(nullable SDWebImageContext *)context
                  progress:(nullable SDImageLoaderProgressBlock)progressBlock
                 completed:(nullable SDExternalCompletionBlock)completedBlock {
    [self sd_internalSetImageWithURL:url
                    placeholderImage:placeholder
                             options:options
                             context:context
                       setImageBlock:nil
                            progress:progressBlock
                           completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
                               if (completedBlock) {
                                   completedBlock(image, error, cacheType, imageURL);
                               }
                           }];
}

所以我们又跑到UIView+WebCache.h里研究,其实UIImageView+WebCache.h的底层是调用的UIView+WebCache.h里的
- (void)sd_internalSetImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options context:(nullable SDWebImageContext *)context setImageBlock:(nullable SDSetImageBlock)setImageBlock progress:(nullable SDImageLoaderProgressBlock)progressBlock completed:(nullable SDInternalCompletionBlock)completedBlock;
一共需要传递七个参数,重点看一下SDWebImageOptions这个枚举
https://sdwebimage.github.io/Enums/SDWebImageOptions.html

/// WebCache options
typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
    /**
     * By default, when a URL fail to be downloaded, the URL is blacklisted so the library won't keep trying.
     * This flag disable this blacklisting.
     默认情况下,当URL无法下载时,URL会被列入黑名单,因此库不会继续尝试。此标志禁用此黑名单。
     */
    SDWebImageRetryFailed = 1 << 0,
    
    /**
     * By default, image downloads are started during UI interactions, this flags disable this feature,
     * leading to delayed download on UIScrollView deceleration for instance.
     默认情况下,图像下载是在UI交互期间启动的,此标志禁用此功能,导致UIScrollView减速下载延迟下载。
     */
    SDWebImageLowPriority = 1 << 1,
    
    /**
     * This flag enables progressive download, the image is displayed progressively during download as a browser would do.
     * By default, the image is only displayed once completely downloaded.
     此标志启用渐进式下载,图像在下载过程中逐步显示,就像浏览器一样。默认情况下,图像仅在完全下载后显示。
     */
    SDWebImageProgressiveLoad = 1 << 2,
    
    /**
     * Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
     * The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
     * This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
     * If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
     *
     * Use this flag only if you can't make your URLs static with embedded cache busting parameter.
  即使图像被缓存,也要尊重HTTP响应缓存控件,并在需要时从远程位置刷新图像。磁盘缓存将由NSURLCache而不是SDWebImage处理,从而导致轻微的性能下降。此选项有助于处理在同一请求URL后面更改的图像,例如Facebook图形api配置文件图片。如果刷新缓存的图像,则使用缓存的图像调用完成块一次,并再次使用最终图像调用完成块。
     
     仅当您无法使用嵌入式缓存清除参数使URL保持静态时,才使用此标志。
     */
    SDWebImageRefreshCached = 1 << 3,
    
    /**
     * In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
     * extra time in background to let the request finish. If the background task expires the operation will be cancelled.
  在iOS4+中,如果应用程序转到后台,请继续下载图像。这是通过向系统询问后台的额外时间以完成请求来实现的。如果后台任务到期,则操作将被取消。
     */
    SDWebImageContinueInBackground = 1 << 4,
    
    /**
     * Handles cookies stored in NSHTTPCookieStore by setting
     * NSMutableURLRequest.HTTPShouldHandleCookies = YES;
     通过设置NSMutableURLRequest.HTTPShouldHandleCookies = YES处理存储在NSHTTPCookieStore中的cookie;
     */
    SDWebImageHandleCookies = 1 << 5,
    
    /**
     * Enable to allow untrusted SSL certificates.
     * Useful for testing purposes. Use with caution in production.
     启用以允许不受信任的SSL证书。用于测试目的。在生产中谨慎使用。
     */
    SDWebImageAllowInvalidSSLCertificates = 1 << 6,
    
    /**
     * By default, images are loaded in the order in which they were queued. This flag moves them to
     * the front of the queue.
     默认情况下,图像按其排队顺序加载。此标志将它们移动到队列的前面。
     */
    SDWebImageHighPriority = 1 << 7,
    
    /**
     * By default, placeholder images are loaded while the image is loading. This flag will delay the loading
     * of the placeholder image until after the image has finished loading.
     默认情况下,在加载图像时会加载占位符图像。此标志将延迟加载占位符图像,直到图像完成加载。
     */
    SDWebImageDelayPlaceholder = 1 << 8,
    
    /**
     * We usually don't apply transform on animated images as most transformers could not manage animated images.
     * Use this flag to transform them anyway.
     我们通常不对动画图像应用变换,因为大多数变形金刚无法管理动画图像。无论如何,使用此标志来转换它们。
     */
    SDWebImageTransformAnimatedImage = 1 << 9,
    
    /**
     * By default, image is added to the imageView after download. But in some cases, we want to
     * have the hand before setting the image (apply a filter or add it with cross-fade animation for instance)
     * Use this flag if you want to manually set the image in the completion when success
 默认情况下,下载后将图像添加到imageView。但在某些情况下,我们希望在设置图像之前握手(例如应用过滤器或使用交叉渐变动画添加过滤器)如果要在成功时手动设置图像,请使用此标志
     */
    SDWebImageAvoidAutoSetImage = 1 << 10,
    
    /**
     * By default, images are decoded respecting their original size. On iOS, this flag will scale down the
     * images to a size compatible with the constrained memory of devices.
     * This flag take no effect if `SDWebImageAvoidDecodeImage` is set. And it will be ignored if `SDWebImageProgressiveLoad` is set.
     默认情况下,图像会根据其原始大小进行解码。在iOS上,此标志会将图像缩小到与设备的受限内存兼容的大小。如果SDWebImageAvoidDecodeImage设置,此标志不起作用。如果SDWebImageProgressiveLoad设置它将被忽略。
     */
    SDWebImageScaleDownLargeImages = 1 << 11,
    
    /**
     * By default, we do not query image data when the image is already cached in memory. This mask can force to query image data at the same time. However, this query is asynchronously unless you specify `SDWebImageQueryMemoryDataSync`
     默认情况下,当图像已缓存在内存中时,我们不会查询图像数据。此掩码可以强制同时查询图像数据。但是,除非您指定,否则此查询是异步的SDWebImageQueryMemoryDataSync
     */
    SDWebImageQueryMemoryData = 1 << 12,
    
    /**
     * By default, when you only specify `SDWebImageQueryMemoryData`, we query the memory image data asynchronously. Combined this mask as well to query the memory image data synchronously.
     * @note Query data synchronously is not recommend, unless you want to ensure the image is loaded in the same runloop to avoid flashing during cell reusing.
  默认情况下,当您仅指定时SDWebImageQueryMemoryData,我们异步查询内存映像数据。结合此掩码以同步查询存储器图像数据。
     建议不要同步查询数据,除非您希望确保将图像加载到同一个runloop中以避免在单元重用期间闪烁。
     */
    SDWebImageQueryMemoryDataSync = 1 << 13,
    
    /**
     * By default, when the memory cache miss, we query the disk cache asynchronously. This mask can force to query disk cache (when memory cache miss) synchronously.
     * @note These 3 query options can be combined together. For the full list about these masks combination, see wiki page.
     * @note Query data synchronously is not recommend, unless you want to ensure the image is loaded in the same runloop to avoid flashing during cell reusing.
  默认情况下,当内存缓存未命中时,我们会异步查询磁盘缓存。此掩码可以强制同步查询磁盘高速缓存(当内存高速缓存未命中时)。
     这三个查询选项可以组合在一起。有关这些蒙版组合的完整列表,请参阅Wiki页面。
     建议不要同步查询数据,除非您希望确保将图像加载到同一个runloop中以避免在单元重用期间闪烁。
     */
    SDWebImageQueryDiskDataSync = 1 << 14,
    
    /**
     * By default, when the cache missed, the image is load from the loader. This flag can prevent this to load from cache only.
     
     默认情况下,当缓存丢失时,图像从加载器加载。此标志可以防止仅从缓存加载。
     */
    SDWebImageFromCacheOnly = 1 << 15,
    
    /**
     * By default, we query the cache before the image is load from the loader. This flag can prevent this to load from loader only.
     默认情况下,我们在从加载器加载图像之前查询缓存。此标志可以防止仅从加载程序加载。
     */
    SDWebImageFromLoaderOnly = 1 << 16,
    
    /**
     * By default, when you use `SDWebImageTransition` to do some view transition after the image load finished, this transition is only applied for image download from the network. This mask can force to apply view transition for memory and disk cache as well.
     默认情况下,在SDWebImageTransition图像加载完成后用于执行某些视图转换时,此转换仅适用于从网络下载图像。此掩码也可以强制为内存和磁盘缓存应用视图转换。
     */
    SDWebImageForceTransition = 1 << 17,
    
    /**
     * By default, we will decode the image in the background during cache query and download from the network. This can help to improve performance because when rendering image on the screen, it need to be firstly decoded. But this happen on the main queue by Core Animation.
     * However, this process may increase the memory usage as well. If you are experiencing a issue due to excessive memory consumption, This flag can prevent decode the image.
     默认情况下,我们将在缓存查询期间在后台解码图像并从网络下载。这有助于提高性能,因为在屏幕上渲染图像时,需要首先对其进行解码。但这发生在Core Animation的主队列中。但是,此过程也可能会增加内存使用量。如果由于过多的内存消耗而遇到问题,此标志可以防止解码图像。
     */
    SDWebImageAvoidDecodeImage = 1 << 18,
    
    /**
     * By default, we decode the animated image. This flag can force decode the first frame only and produece the static image.
     默认情况下,我们解码动画图像。该标志只能强制解码第一帧并生成静态图像。
     */
    SDWebImageDecodeFirstFrameOnly = 1 << 19,
    
    /**
     * By default, for `SDAnimatedImage`, we decode the animated image frame during rendering to reduce memory usage. However, you can specify to preload all frames into memory to reduce CPU usage when the animated image is shared by lots of imageViews.
     * This will actually trigger `preloadAllAnimatedImageFrames` in the background queue(Disk Cache & Download only).
     默认情况下,SDAnimatedImage我们在渲染过程中解码动画图像帧以减少内存使用量。但是,您可以指定将所有帧预加载到内存中,以在大量imageView共享动画图像时降低CPU使用率。这实际上将preloadAllAnimatedImageFrames在后台队列中触发(仅限磁盘缓存和下载)。
     */
    SDWebImagePreloadAllFrames = 1 << 20,
    
    /**
     * By default, when you use `SDWebImageContextAnimatedImageClass` context option (like using `SDAnimatedImageView` which designed to use `SDAnimatedImage`), we may still use `UIImage` when the memory cache hit, or image decoder is not available to produce one exactlly matching your custom class as a fallback solution.
     * Using this option, can ensure we always callback image with your provided class. If failed to produce one, a error with code `SDWebImageErrorBadImageData` will been used.
     * Note this options is not compatible with `SDWebImageDecodeFirstFrameOnly`, which always produce a UIImage/NSImage.
     默认情况下,当您使用SDWebImageContextAnimatedImageClass上下文选项(如使用SDAnimatedImageView设计使用的选项SDAnimatedImage)时,我们仍然可以UIImage在内存缓存命中时使用,或者图像解码器不可用于生成与您的自定义类完全匹配的后备解决方案。使用此选项,可以确保我们始终使用您提供的类回调图像。如果未能生成一个,SDWebImageErrorBadImageData将使用代码错误。请注意,此选项与之不兼容SDWebImageDecodeFirstFrameOnly,它始终生成UIImage / NSImage。
     */
    SDWebImageMatchAnimatedImageClass = 1 << 21,
};

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容