123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- /*
- * This file is part of the SDWebImage package.
- * (c) Olivier Poitrey <rs@dailymotion.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- #import "UIView+WebCacheOperation.h"
- #if SD_UIKIT || SD_MAC
- #import "objc/runtime.h"
- static char loadOperationKey;
- typedef NSMutableDictionary<NSString *, id> SDOperationsDictionary;
- @implementation UIView (WebCacheOperation)
- - (SDOperationsDictionary *)operationDictionary {
- // SDOperationsDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
- // if (operations) {
- // return operations;
- // }
- // operations = [NSMutableDictionary dictionary];
- // objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- // return operations;
- @synchronized(self) {
- SDOperationsDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
- if (operations) {
- return operations;
- }
- operations = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0];
- objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- return operations;
- }
- }
- - (void)sd_setImageLoadOperation:(nullable id)operation forKey:(nullable NSString *)key {
- // if (key) {
- // [self sd_cancelImageLoadOperationWithKey:key];
- // if (operation) {
- // SDOperationsDictionary *operationDictionary = [self operationDictionary];
- // operationDictionary[key] = operation;
- // }
- // }
- if (key) {
- [self sd_cancelImageLoadOperationWithKey:key];
- if (operation) {
- SDOperationsDictionary *operationDictionary = [self operationDictionary];
- @synchronized (self) {
- [operationDictionary setObject:operation forKey:key];
- }
- }
- }
- }
- - (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key {
- // Cancel in progress downloader from queue
- // SDOperationsDictionary *operationDictionary = [self operationDictionary];
- // id operations = operationDictionary[key];
- // if (operations) {
- // if ([operations isKindOfClass:[NSArray class]]) {
- // for (id <SDWebImageOperation> operation in operations) {
- // if (operation) {
- // [operation cancel];
- // }
- // }
- // } else if ([operations conformsToProtocol:@protocol(SDWebImageOperation)]){
- // [(id<SDWebImageOperation>) operations cancel];
- // }
- // [operationDictionary removeObjectForKey:key];
- // }
- if (key) {
- // Cancel in progress downloader from queue
- SDOperationsDictionary *operationDictionary = [self operationDictionary];
- id<SDWebImageOperation> operation;
-
- @synchronized (self) {
- operation = [operationDictionary objectForKey:key];
- }
- if (operation) {
- if ([operation conformsToProtocol:@protocol(SDWebImageOperation)]) {
- [operation cancel];
- }
- @synchronized (self) {
- [operationDictionary removeObjectForKey:key];
- }
- }
- }
- }
- - (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key {
- if (key) {
- SDOperationsDictionary *operationDictionary = [self operationDictionary];
- [operationDictionary removeObjectForKey:key];
- }
- }
- @end
- #endif
|