源码阅读:SDWebImage(十七)——UIView+WebCacheOperation

该文章阅读的SDWebImage的版本为4.3.3。

这个分类提供的方法用于在框架内部取消UIView上图像的加载。

1.公共方法

/**
 保存图像的加载操作对象
 */
- (void)sd_setImageLoadOperation:(nullable id<SDWebImageOperation>)operation forKey:(nullable NSString *)key;
复制代码
/**
 取消key对应的所有操作
 */
- (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key;
复制代码
/**
 删除key对应的保存的所有操作但不取消
 */
- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key;
复制代码

2.私有静态变量

/**
 用于关联对象的key
 */
static char loadOperationKey;
复制代码

3.私有类型定义

/**
 定义了用于保存key和与之关联的操作对象的集合类型
 */
typedef NSMapTable<NSString *, id<SDWebImageOperation>> SDOperationsDictionary;
复制代码

4.关联对象方法

/**
 获取保存操作对象的字典
 */
- (SDOperationsDictionary *)sd_operationDictionary {
    // 加锁
    @synchronized(self) {
        // 通过关联对象获取保存操作对象的字典
        SDOperationsDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
        // 如果有就直接返回
        if (operations) {
            return operations;
        }
        // 如果没有就创建key是强引用,value是弱引用的保存操作对象的字典
        operations = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0];
        // 通过关联对象保存保存操作对象的字典
        objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        // 返回保存操作对象的字典
        return operations;
    }
}
复制代码

5.实现

- (void)sd_setImageLoadOperation:(nullable id<SDWebImageOperation>)operation forKey:(nullable NSString *)key {
    // 必须要传key
    if (key) {
        // 先取消掉key对应的所有操作
        [self sd_cancelImageLoadOperationWithKey:key];
        // 如果传了操作对象
        if (operation) {
            // 获取到保存操作对象的字典
            SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
            // 加锁
            @synchronized (self) {
                // 赋值保存
                [operationDictionary setObject:operation forKey:key];
            }
        }
    }
}
复制代码
- (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key {
    // 获取到保存操作对象的字典
    SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
    // 获取到key对应的操作对象
    id<SDWebImageOperation> operation;
    @synchronized (self) {
        operation = [operationDictionary objectForKey:key];
    }
    if (operation) {
        // 调用操作对象的取消操作
        if ([operation conformsToProtocol:@protocol(SDWebImageOperation)]){
            [operation cancel];
        }
        // 从保存操作对象的字典中移除key对应的操作
        @synchronized (self) {
            [operationDictionary removeObjectForKey:key];
        }
    }
}
复制代码
- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key {
    // 必须要传key
    if (key) {
        // 获取到保存操作对象的字典
        SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
        // 直接从保存操作对象的字典中移除key对应的操作
        @synchronized (self) {
            [operationDictionary removeObjectForKey:key];
        }
    }
}
复制代码

6.总结

这个分类的作用比较简单,就是提供了根据key取消对象加载操作的功能。

源码阅读系列:SDWebImage

源码阅读:SDWebImage(一)——从使用入手

源码阅读:SDWebImage(二)——SDWebImageCompat

源码阅读:SDWebImage(三)——NSData+ImageContentType

源码阅读:SDWebImage(四)——SDWebImageCoder

源码阅读:SDWebImage(五)——SDWebImageFrame

源码阅读:SDWebImage(六)——SDWebImageCoderHelper

源码阅读:SDWebImage(七)——SDWebImageImageIOCoder

源码阅读:SDWebImage(八)——SDWebImageGIFCoder

源码阅读:SDWebImage(九)——SDWebImageCodersManager

源码阅读:SDWebImage(十)——SDImageCacheConfig

源码阅读:SDWebImage(十一)——SDImageCache

源码阅读:SDWebImage(十二)——SDWebImageDownloaderOperation

源码阅读:SDWebImage(十三)——SDWebImageDownloader

源码阅读:SDWebImage(十四)——SDWebImageManager

源码阅读:SDWebImage(十五)——SDWebImagePrefetcher

源码阅读:SDWebImage(十六)——SDWebImageTransition

源码阅读:SDWebImage(十七)——UIView+WebCacheOperation

源码阅读:SDWebImage(十八)——UIView+WebCache

源码阅读:SDWebImage(十九)——UIImage+ForceDecode/UIImage+GIF/UIImage+MultiFormat

源码阅读:SDWebImage(二十)——UIButton+WebCache

源码阅读:SDWebImage(二十一)——UIImageView+WebCache/UIImageView+HighlightedWebCache

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值