1024programmer Java SDWebImage source code analysis (4) Categories

SDWebImage source code analysis (4) Categories

Foreword

Jianshu.com/p/9bed61940c57
The Chinese annotation code demo of this article is updated on my github.

The previous article explained the Utils part of SDWebImage. This article will talk about the final Categories part.

Categories

Categories contains the following class files:
* MKAnnotationView+WebCache
* NSData+ImageContentType
* UIButton+WebCache
* UIImage+GIF
* UIImage+MultiFormat
* UIImage+WebP
* UIImageView+HighlightedWebCache
* UIImageView+WebCache
* UIView+WebCacheOperation

Here I will only introduce UIView+WebCacheOperation and UIImageView+WebCache. Other files are similar, so I won’t expand on them here. I will try my best to read them in the comments on github. Again.

UIView+WebCacheOperation

UIView+WebCacheOperation mainly uses associateObject to cache, cancel, and delete running operations.
The relevant methods are defined as follows:

/**
Set the load operation of the image (store it in a uiview dictionary)

@param operation operation
@param key stored key
*/

- (void)sd_setImageLoadOperation:(id)operation forKey:(NSString *)key;

/**
Cancel the operation of the current uiview's key

@param key The stored key
*/

- (void)sd_cancelImageLoadOperationWithKey:(NSString *)key;

/* *
Remove the key operation of the current uiview and do not cancel them

@param key The stored key
*/

- (void)sd_removeImageLoadOperationWithKey:(NSString *)key;

Related implementations:

static char loadOperationKey;

- (NSMutableDictionary *)operationDictionary {
//Save and get dictionaryu through associateobject
NSMutableDictionary *operatiOns= objc_getAssociatedObject(self, &loadOperationKey);
if (operations) {
return operations;
}
operatiOns= [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return operations;
}

- (void)sd_setImageLoadOperation:(id)operation forKey:(NSString *)key {
//Cancel first Operation
[self sd_cancelImageLoadOperationWithKey:key];
//Then get the dictionary and store it
NSMutableDictionary *operatiOnDictionary= [self operationDictionary];
[operationDictionary setObject :operation forKey:key];
}

- (void)sd_cancelImageLoadOperationWithKey:(NSString *)key {
// Cancel in progress downloader from queue
//Get dictionary, And cancel the operation in the queue
NSMutableDictionary *operatiOnDictionary= [self operationDictionary] ;
id operatiOns= [operationDictionary objectForKey:key];
if (operations) {
//If it is an array, get all the cancellations inside
if ([operations isKindOfClass:[ NSArray class]]) {
for (id operation in operations) {
if (operation) {
[operation cancel];
}
}
//If you are implementing the SDWebImageOperation contract, cancel it directly
} else if ([operations conformsToProtocol:@protocol(SDWebImageOperation)]){
[(id) operations cancel];
}
[operationDictionary removeObjectForKey:key];
}
}

- (void)sd_removeImageLoadOperationWithKey:(NSString *)key {
// Get the dictionary and delete the key directly
NSMutableDictionary *operatiOnDictionary= [self operationDictionary];
[operationDictionary removeObjectForKey:key];
}

UIImageView+WebCache

UIImageView+WebCache is the category of sdwebimage used by UIImage, the outermost calling method for users.
Here is the definition and implementation of the most basic operating methods:

/**
Set the image, placeholder and settings of imageview through url

Download method is asynchronous and cached

@param url image url
@param placeholder initialize image
@param options download image method
@param progressBlock progress block callback
@param completedBlock complete block Callback
*/

- (void)sd_setImageWithURL:(NSURL * )url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
//Cancel the current image download
[self sd_cancelCurrentImageLoad];
//Save url
objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

//If there is no SDWebImageDelayPlaceholder, set the placeholder diagram first
if (!(options & SDWebImageDelayPlaceholder)) {
dispatch_main_async_safe(^ {
self.image = placeholder;
});
}

//There is a url for download operation
if (url) {

// check if activityView is enabled or not
//Whether the loading label image needs to be displayed
if ([self showActivityIndicatorView]) {
[self addActivityIndicator];
}

//Download pictures
__weak __typeof(self)wself = self;
id operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {

//Remove possible loading images span>
[wself removeActivityIndicator];
if (!wself) return;
dispatch_main_sync_safe(^{
if (!wself) return;

//Do not automatically set the image
if (image && (options & SDWebImageAvoidAutoSetImage) && completedBlock)
{
completedBlock(image, error, cacheType, url);
return;
}
//Set image
else if (image) {
wself.image = image;
[wself setNeedsLayout];
} else {
//There is no image to set the default image
if ((options & SDWebImageDelayPlaceholder)) {
wself.image = placeholder;
[wself setNeedsLayout];
}
}
if (completedBlock && finished) {
completedBlock(image, error, cacheType, url);
}
});
}];
//The key of cache operation is load
[self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];
} else {
//If there is no url, remove the loading directly Figure
dispatch_main_async_safe(^{
[self removeActivityIndicator];
//Return Error to the outside block
if (completedBlock) {
NSError * error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
completedBlock(nil, error, SDImageCacheTypeNone, url);
}
});
}
}

Summary

At this point, all SDWebImage has been parsed.
I personally feel that the most important thing is to process some details of images, such as gif images, decode images, exchanging space for time, etc. The implementation of these details is the basis of the entire image processing.
In the framework, SDWebImage also carries out multi-layer encapsulation, encapsulating the most basic network operations into operations, and then encapsulates a cache layer on top of this, and uses a manager to manage it. The user’s usage calls implement the corresponding methods in the category.

It is said that version 4.0 is being worked on now, and there will be some big changes, so you can look forward to it.

References

1. Brief analysis of SDWebImage source code

f setNeedsLayout];
}
}
if (completedBlock && finished) {
completedBlock(image, error, cacheType, url);
}
});
}];
//The key of the cache operation is load
[self sd_setImageLoadOperation:operation forKey:@“UIImageViewImageLoad”];
} else {
//Remove the loading image without url
dispatch_main_async_safe(^{
[self removeActivityIndicator];
//Return errors to the outside block
if (completedBlock) {
NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @“Trying to load a nil url”}];
completedBlock (nil, error, SDImageCacheTypeNone, url);
}
});
}
}

Summary

At this point, all SDWebImage has been parsed.
I personally feel that the most important thing is to process some details of images, such as gif images, decode images, exchanging space for time, etc. The implementation of these details is the basis of the entire image processing.
In the framework, SDWebImage also carries out multi-layer encapsulation, encapsulating the most basic network operations into operations, and then encapsulates a cache layer on top of this, and uses a manager to manage it. The user’s usage calls implement the corresponding methods in the category.

It is said that version 4.0 is being worked on now, and there will be some big changes, so you can look forward to it.

References

1. Brief analysis of SDWebImage source code

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/759217

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索