標籤:nat tom 添加 amp ima ide nil ret ring
這個分類主要用來對 UIView 的映像下載操作添加、取消和移除。
.h
1 * Set the image load operation (storage in a UIView based dictionary)2 *3 * @param operation the operation4 * @param key key for storing the operation5 */6 - (void)sd_setImageLoadOperation:(nullable id)operation forKey:(nullable NSString *)key;
設定映像載入操作。
1 /**2 * Cancel all operations for the current UIView and key3 *4 * @param key key for identifying the operations5 */6 - (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key;
根據 key 取消 UIView 的所有當前操作。
1 /**2 * Just remove the operations corresponding to the current UIView and key without cancelling them3 *4 * @param key key for identifying the operations5 */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_setAssociatedObject10 */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_setAssociatedObject10 * @see objc_removeAssociatedObjects11 */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);
SDWebImage源碼閱讀(十五)UIView+WebCacheOperation