IOS开发笔记之浅读SDWebImage实现原理

2016-09-06  本文已影响0人  一只小猪的360

SDWebImage内部实现过程(新版本在各方法前加上了sd_前缀,以区分UIImageView+AFNetworking中的方法)

覆盖方法,指哪打哪,这个方法是下载imagePath2的时候响应

SDWebImageManager *manager = [SDWebImageManager sharedManager]; [manager downloadImageWithURL:imagePath2 options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) { NSLog(@"显示当前进度"); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { NSLog(@"下载完成"); }];

当服务器更新了某一张图片资源时,客户端需要重新加载,那么就可以设置SDWebImageOption为SDWebImageRefreshCached;附上全部的SDWebImageOptions

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

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

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

/**
 * 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 embeded cache busting parameter.
 */
SDWebImageRefreshCached = 1 << 4,

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

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

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

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

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

/**
 * 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,
};
上一篇下一篇

猜你喜欢

热点阅读