Original link: http://blog.csdn.net/indulgein/article/details/51130812
1. Basic principles of SDWebImage
SDWebImage is currently one of the most popular three-party picture processing frameworks, and its power is believed to be experienced by iOS developers only with a single line of code
-
[[UIImageView new] sd_setImageWithURL:(NSURL *) placeholderImage:(UIImage *) options:(SDWebImageOptions) progress:^(NSInteger receivedSize, NSInteger expectedSize) {
-
-
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
-
-
}];
You can load and display pictures asynchronously, and it automatically adds a caching strategy for us, not to mention here.
However, the process from invoking this method to displaying pictures is not as simple as beginners imagine. We can guess roughly from its many parameters. This method is definitely not just a network request. Here's a simple explanation of the principles behind this method:
1. Display placeholderImage
2.SDImageCache looks in the cache for pictures that have been downloaded
3. Find out if there are any pictures from the memory picture cache first
4. Display pictures if there is a picture cache in memory
5. If not in memory, generate NSInvocationOperation s to add to the execution queue and start looking for picture caches from the hard disk
6. If there is any on the hard disk, add pictures to the memory cache (if the free memory is too small, the memory cache will be emptied first) and display the pictures
7. If there is not one on your hard disk, it means that the picture is not cached and you need to download the picture, share or regenerate a downloader, SDWebImageDownLoader, to start downloading the picture
8. Start a picture network request and download data
9. Deliver the data download to SDWebImageDecoder for picture decoding
10. Callback display picture
11. Save pictures to hard disk cache and memory cache
12.SDImageCache initialization registers notifications to clear the memory picture cache when memory warnings or backs up, and expired pictures when applications are finished
Do you feel the tightness of its logic and the maximum use of its resources?Yes, that's one reason why SDWebImage has been so successful.
2. Basic use of SDWebImage
(1) The simplest call
-
[[UIImageView new] sd_setImageWithURL:(NSURL *) placeholderImage:(UIImage *) options:(SDWebImageOptions) progress:^(NSInteger receivedSize, NSInteger expectedSize) {
-
-
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
-
-
}];
It is worth noting that the first progress block returns 100% of the monitored image download, and callback processing occurs in the second.
(2) Independent Download
However, if we only need to download pictures and not cache, we often use another class to achieve results:
-
SDWebImageDownloaderOperation *operation = [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:(NSURL *) options:(SDWebImageDownloaderOptions) progress:^(NSInteger receivedSize, NSInteger expectedSize) {
-
-
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
-
-
}];
This method has a return value, which is an id type that follows the SDWebImageOperation protocol. Here we use the SDWebImageDownloaderOperation type to receive because it complies with the SDWebImageOperation protocol.
The resulting operation has many properties, including the url of the downloaded resource, priority, whether or not you have a certificate, and so on.
Normally, we don't need to care about the operation's content. Normal needs can be handled in just two block s.
(3) Independent caching
1. Store in Cache
-
-
[[SDImageCache sharedImageCache] storeImage:(UIImage *) forKey:(NSString *) toDisk:(BOOL)];
-
[[SDImageCache sharedImageCache] storeImage:(UIImage *) forKey:(NSString *)];
-
[[SDImageCache sharedImageCache] storeImage:(UIImage *) recalculateFromImage:(BOOL) imageData:(NSData *) forKey:(NSString *) toDisk:(BOOL)];
2. Read Cache
-
-
[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:(NSString *)];
-
[[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:(NSString *)];
(4) SDWebImageManager
Management class has a very powerful function, it can achieve independent caching, independent download, Collection Download and caching methods, as well as internal implementation of management operations.
1. Stand-alone download and caching
-
-
[SDWebImageManager sharedManager].imageCache
-
[SDWebImageManager sharedManager].imageDownloader
2. Manage operations (supervision of picture processing threads)
-
-
[SDWebImageManager sharedManager].isRunning;
-
-
[[SDWebImageManager sharedManager] cancelAll];
-
-
[[SDWebImageManager sharedManager] cachedImageExistsForURL:(NSURL *)];
-
[SDWebImageManager sharedManager] cachedImageExistsForURL:(NSURL *) completion:^(BOOL isInCache) {
-
-
};
-
-
[[SDWebImageManager sharedManager] diskImageExistsForURL:(NSURL *)];
-
[SDWebImageManager sharedManager] diskImageExistsForURL:(NSURL *) completion:^(BOOL isInCache) {
-
-
};
-
-
[[SDWebImageManager sharedManager] cacheKeyForURL:(NSURL *)];
(5) Other uses
SDWebImage has other features, especially since the last version was updated, and I haven't studied them too deeply, because I think the methods listed above can already face most of the problems in actual development. If you want to know more about other methods, you can go to the open source code of SDWebImage with detailed comments., this is just for beginners to find easily.