UIView+WebCacheOperation.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "UIView+WebCacheOperation.h"
  9. #import "objc/runtime.h"
  10. static char loadOperationKey;
  11. @implementation UIView (WebCacheOperation)
  12. - (NSMutableDictionary *)operationDictionary {
  13. NSMutableDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
  14. if (operations) {
  15. return operations;
  16. }
  17. operations = [NSMutableDictionary dictionary];
  18. objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  19. return operations;
  20. }
  21. - (void)sd_setImageLoadOperation:(id)operation forKey:(NSString *)key {
  22. [self sd_cancelImageLoadOperationWithKey:key];
  23. NSMutableDictionary *operationDictionary = [self operationDictionary];
  24. [operationDictionary setObject:operation forKey:key];
  25. }
  26. - (void)sd_cancelImageLoadOperationWithKey:(NSString *)key {
  27. // Cancel in progress downloader from queue
  28. NSMutableDictionary *operationDictionary = [self operationDictionary];
  29. id operations = [operationDictionary objectForKey:key];
  30. if (operations) {
  31. if ([operations isKindOfClass:[NSArray class]]) {
  32. for (id <SDWebImageOperation> operation in operations) {
  33. if (operation) {
  34. [operation cancel];
  35. }
  36. }
  37. } else if ([operations conformsToProtocol:@protocol(SDWebImageOperation)]){
  38. [(id<SDWebImageOperation>) operations cancel];
  39. }
  40. [operationDictionary removeObjectForKey:key];
  41. }
  42. }
  43. - (void)sd_removeImageLoadOperationWithKey:(NSString *)key {
  44. NSMutableDictionary *operationDictionary = [self operationDictionary];
  45. [operationDictionary removeObjectForKey:key];
  46. }
  47. @end