SDWebImage最新的使用

2015年8月21日
SDWebImage的使用

一、下载SDWebImage,导入工程。

github托管地址https://github.com/rs/SDWebImage

二、导入头文件

#import "UIImageView+WebCache.h"

三、图片缓存方法

  • (1)、

    - (void)setImageWithURL:(NSURL *)url;
    

    例如:

[imageView setImageWithURL:url]
  • (2)、

    - (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock
    

    例如:

//用block 可以在图片加载完成之后做些事情
[imageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
     NSLog(@"这里可以在图片加载完成之后做些事情");
}];
  • (3)、

    - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
    

    例如:

//给一张默认图片,先使用默认图片,当图片加载完成后再替换
[imageView sd_setImageWithURL:url placeholderImage:image];
  • (4)、

    - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock
    

    例如:

//使用默认图片,而且用block 在完成后做一些事情
[imageView sd_setImageWithURL:url placeholderImage:image completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
     NSLog(@"这里可以在图片加载完成之后做些事情");
}];
  • (5)、

    - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
    

    例如:

//options选择方式
[imageView sd_setImageWithURL:url placeholderImage:image options: SDWebImageCacheMemoryOnly];

注释:其中options是选择方式,其是个枚举值 SDWebImageOptions,具体下面介绍;

四、options所有选项

(1)、

/**
     * 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.
 */
SDWebImageRetryFailed = 1 << 0,

//默认情况下,如果一个url在下载的时候失败了,那么这个url会被加入黑名单并且library不会尝试再次下载,这个flag会阻止library把失败的url加入黑名单(简单来说如果选择了这个flag,那么即使某个url下载失败了,sdwebimage还是会尝试再次下载它)。

(2)、

/**
     * By default, image downloads are started during UI interactions, this flags disable this feature,
     * leading to delayed download on UIScrollView deceleration for instance.
 */
SDWebImageLowPriority = 1 << 1,

//默认情况下,图片会在交互发生的时候下载(例如你滑动tableview的时候),这个flag会禁止这个特性,导致的结果就是在scrollview减速的时候才会开始下载(也就是你滑动的时候scrollview不下载,你手从屏幕上移走,scrollview开始减速的时候才会下载图片)。

(3)、

/**
     * This flag disables on-disk caching
 */
SDWebImageCacheMemoryOnly = 1 << 2,

//这个flag禁止磁盘缓存,只有内存缓存

(4)、

/**
 * 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.
 */
SDWebImageProgressiveDownload = 1 << 3

//这个flag会在图片下载的时候显示(就像你用浏览器浏览网页的时候那种图片下载,一截一截的显示(待确认))

(5)、

/**
 * 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.
 */
SDWebImageRefreshCached = 1 << 4

(6)、

/**
 * 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.
 */
SDWebImageContinueInBackground = 1 << 5

(7)、

/**
 * Handles cookies stored in NSHTTPCookieStore by setting
 * NSMutableURLRequest.HTTPShouldHandleCookies = YES;
 */
SDWebImageHandleCookies = 1 << 6

(8)、

/**
 * Enable to allow untrusted SSL certificates.
 * Useful for testing purposes. Use with caution in production.
 */
SDWebImageAllowInvalidSSLCertificates = 1 << 7

(9)、

/**
 * By default, image are loaded in the order they were queued. This flag move them to
 * the front of the queue and is loaded immediately instead of waiting for the current queue to be loaded (which
 * could take a while).
 */
SDWebImageHighPriority = 1 << 8

(10)、

/**
 * 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 << 9

(11)、

/**
 * We usually don't call transformDownloadedImage delegate method on animated images,
 * as most transformation code would mangle it.
 * Use this flag to transform them anyway.
 */
SDWebImageTransformAnimatedImage = 1 << 10

(12)、

/**
 * 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
 */
SDWebImageAvoidAutoSetImage = 1 << 11

四、SDWebImage内部实现过程

  • (1)、入口setImageWithURL:placeholderImage:options:会先把placeholderImage显示,然后SDWebImageManager根据URL开始处理图片;
  • (2)、进入SDWebImageManager-downloadWithURL:delegate:options:userInfo:,交给SDImageCache从缓存查找图片是否已经下下载,queryDiskCacheForKey:delegate:userInfo:;
  • (3)、先从内存图片缓存查找是否有图片,如果内存中已经有图片缓存,SDImageCacheDelegate回调imageCache:didFindImage:forKey:userInfo:到SDWebImageManager;
  • (4)、SDWebImageManagerDelegate回到webImageManager:didFinishWithImage:到UIImageView+WebCache等前端展示图片;
  • (5)、如果内存缓存中没有,生成NSInvocationOperation添加到队列开始从硬盘查找图片是否已经缓存;
  • (6)、根据URLKey在硬盘缓存目录下尝试读取图片文件。这一步是在NSOperation进行的操作,所有回主线程进行结果回调notifyDelegate;
  • (7)、如果上一操作从硬盘读取到了图片,将图片添加到内存缓存中(如果空闲内存过小,会先清空内存缓存)。SDImageCacheDelegate回调imageCache:didFindImage:forKey:userInfo:。进而回调显示图片;
  • (8)、如果从硬盘缓存目录读取不到图片,说明所有缓存都不存在该图片,需要下载图片,回调imageCache:didiNotFindImageForKey:userInfo:;
  • (9)、共享或重新生成一个下载器SDWebImageDownloader开始下载图片;
  • (10)、图片下载由NSURLConnection来做,实现相关delegate来判断图片下载中、下载完成和下载失败;
  • (11)、connection:didReceiveDate:中利用ImageIO做了按图片下载进度加载效果;
  • (12)、connectionDidFinishLoading:数据下载完成后交给SDWebImageDecoder做图片解码处理;
  • (13)、图片解码处理在一个NSOperationQueue完成,不会拖慢主线程UI。如果有需要对下载的图片进行二次处理,最好也在这里完成,效果会好很多;
  • (14)、在主线程notifyDelegateOnMainThreadWithInfo:宣告解码完成,imageDecoder:didFinishDecodingImage:userInfo:回调给SDWebImageDownloader;
  • (15)、imageDownloader:didFinishWithImage:回调给SDWebImageManager告知图片下载完成;
  • (16)、通知所有的downloadDelegates下载完成,回调给需要的地方显示图片;
  • (17)、将图片保存到SDImageCache中,内存缓存和硬盘缓存同时存在。写问及那到硬盘也可以单独NSInvocationOperation完成,避免拖慢主线程;
  • (18)、SDImageCache在初始化的时候会注册一些消息通知,在内存警告或推到后台的时候清理内存图片缓存,应用结束的时候清理过期图片;
  • (19)、SDWI也提供了UIButton+WebCache和MKAnnotation+WebCache,方便使用;
  • (20)、SDWebImagePrefetcher可以预先下载图片,方便后续使用。

五、获取图片下载过程

NSURL* imagePath = [NSURL URLWithString:@"http://pic31.nipic.com/20130723/12096623_135908542182_2.jpg"];
//覆盖方法,指哪打哪,这个方法是下载imagePath2的时候响应
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:imagePath options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    NSLog(@"显示当前进度%ld--%ld",receivedSize,expectedSize);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
    NSLog(@"下载完成");
}];

六、清除缓存

1、清除硬盘缓存

[[SDImageCache sharedImageCache]clearDisk];

2、清除内存缓存

[[SDImageCache sharedImageCache]clearMemory];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoxiaobukuang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值