这个分类主要用来对 UIView 的图像下载操作添加、取消和移除。
.h
1 * Set the image load operation (storage in a UIView based dictionary) 2 * 3 * @param operation the operation 4 * @param key key for storing the operation 5 */ 6 - (void)sd_setImageLoadOperation:(nullable id)operation forKey:(nullable NSString *)key;
设置图像加载操作。
1 /** 2 * Cancel all operations for the current UIView and key 3 * 4 * @param key key for identifying the operations 5 */ 6 - (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key;
根据 key 取消 UIView 的所有当前操作。
1 /** 2 * Just remove the operations corresponding to the current UIView and key without cancelling them 3 * 4 * @param key key for identifying the operations 5 */ 6 - (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key;
仅仅根据 key 移除对应的当前 UIView 的操作,并没有取消它们。
.m
1 static char loadOperationKey; 2 3 typedef NSMutableDictionary<NSString *, id> SDOperationsDictionary;
定义一个静态 char loadOperationKey。
定义一个 key 是字符串 value 是id 的可变字典类型。
1 - (SDOperationsDictionary *)operationDictionary { 2 SDOperationsDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey); 3 if (operations) { 4 return operations; 5 } 6 operations = [NSMutableDictionary dictionary]; 7 objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 8 return operations; 9 }
给 UIView 动态添加一个 operationDictionary 属性。
objc_getAssociatedObject
1 /** 2 * Returns the value associated with a given object for a given key. 3 * 4 * @param object The source object for the association. 5 * @param key The key for the association. 6 * 7 * @return The value associated with the key \e key for \e object. 8 * 9 * @see objc_setAssociatedObject 10 */ 11 OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key) 12 OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0);
objc_setAssociatedObject
1 /** 2 * Sets an associated value for a given object using a given key and association policy. 3 * 4 * @param object The source object for the association. 5 * @param key The key for the association. 6 * @param value The value to associate with the key key for object. Pass nil to clear an existing association. 7 * @param policy The policy for the association. For possible values, see “Associative Object Behaviors.” 8 * 9 * @see objc_setAssociatedObject 10 * @see objc_removeAssociatedObjects 11 */ 12 OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) 13 OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0);
时间: 2024-10-06 20:37:25