UIImageView+EMWebCache.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "UIImageView+EMWebCache.h"
  9. #import "objc/runtime.h"
  10. #import "UIView+EMWebCacheOperation.h"
  11. static char imageURLKey;
  12. @implementation UIImageView (EMWebCache)
  13. - (void)sd_setImageWithURL:(NSURL *)url {
  14. [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
  15. }
  16. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
  17. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
  18. }
  19. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(EMSDWebImageOptions)options {
  20. [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
  21. }
  22. - (void)sd_setImageWithURL:(NSURL *)url completed:(EMSDWebImageCompletionBlock)completedBlock {
  23. [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
  24. }
  25. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(EMSDWebImageCompletionBlock)completedBlock {
  26. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
  27. }
  28. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(EMSDWebImageOptions)options completed:(EMSDWebImageCompletionBlock)completedBlock {
  29. [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
  30. }
  31. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(EMSDWebImageOptions)options progress:(EMSDWebImageDownloaderProgressBlock)progressBlock completed:(EMSDWebImageCompletionBlock)completedBlock {
  32. [self sd_cancelCurrentImageLoad];
  33. objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  34. if (!(options & EMSDWebImageDelayPlaceholder)) {
  35. self.image = placeholder;
  36. }
  37. if (url) {
  38. __weak UIImageView *wself = self;
  39. id <EMSDWebImageOperation> operation = [EMSDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, EMSDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  40. if (!wself) return;
  41. dispatch_main_sync_safe(^{
  42. if (!wself) return;
  43. if (image) {
  44. wself.image = image;
  45. [wself setNeedsLayout];
  46. } else {
  47. if ((options & EMSDWebImageDelayPlaceholder)) {
  48. wself.image = placeholder;
  49. [wself setNeedsLayout];
  50. }
  51. }
  52. if (completedBlock && finished) {
  53. completedBlock(image, error, cacheType, url);
  54. }
  55. });
  56. }];
  57. [self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];
  58. } else {
  59. dispatch_main_async_safe(^{
  60. NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
  61. if (completedBlock) {
  62. completedBlock(nil, error, EMSDImageCacheTypeNone, url);
  63. }
  64. });
  65. }
  66. }
  67. - (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(EMSDWebImageOptions)options progress:(EMSDWebImageDownloaderProgressBlock)progressBlock completed:(EMSDWebImageCompletionBlock)completedBlock {
  68. NSString *key = [[EMSDWebImageManager sharedManager] cacheKeyForURL:url];
  69. UIImage *lastPreviousCachedImage = [[EMSDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
  70. [self sd_setImageWithURL:url placeholderImage:lastPreviousCachedImage ?: placeholder options:options progress:progressBlock completed:completedBlock];
  71. }
  72. - (NSURL *)sd_imageURL {
  73. return objc_getAssociatedObject(self, &imageURLKey);
  74. }
  75. - (void)sd_setAnimationImagesWithURLs:(NSArray *)arrayOfURLs {
  76. [self sd_cancelCurrentAnimationImagesLoad];
  77. __weak UIImageView *wself = self;
  78. NSMutableArray *operationsArray = [[NSMutableArray alloc] init];
  79. for (NSURL *logoImageURL in arrayOfURLs) {
  80. id <EMSDWebImageOperation> operation = [EMSDWebImageManager.sharedManager downloadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, EMSDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  81. if (!wself) return;
  82. dispatch_main_sync_safe(^{
  83. __strong UIImageView *sself = wself;
  84. [sself stopAnimating];
  85. if (sself && image) {
  86. NSMutableArray *currentImages = [[sself animationImages] mutableCopy];
  87. if (!currentImages) {
  88. currentImages = [[NSMutableArray alloc] init];
  89. }
  90. [currentImages addObject:image];
  91. sself.animationImages = currentImages;
  92. [sself setNeedsLayout];
  93. }
  94. [sself startAnimating];
  95. });
  96. }];
  97. [operationsArray addObject:operation];
  98. }
  99. [self sd_setImageLoadOperation:[NSArray arrayWithArray:operationsArray] forKey:@"UIImageViewAnimationImages"];
  100. }
  101. - (void)sd_cancelCurrentImageLoad {
  102. [self sd_cancelImageLoadOperationWithKey:@"UIImageViewImageLoad"];
  103. }
  104. - (void)sd_cancelCurrentAnimationImagesLoad {
  105. [self sd_cancelImageLoadOperationWithKey:@"UIImageViewAnimationImages"];
  106. }
  107. @end
  108. @implementation UIImageView (WebCacheDeprecated)
  109. - (NSURL *)imageURL {
  110. return [self sd_imageURL];
  111. }
  112. - (void)setImageWithURL:(NSURL *)url {
  113. [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
  114. }
  115. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
  116. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
  117. }
  118. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(EMSDWebImageOptions)options {
  119. [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
  120. }
  121. - (void)setImageWithURL:(NSURL *)url completed:(EMSDWebImageCompletedBlock)completedBlock {
  122. [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:^(UIImage *image, NSError *error, EMSDImageCacheType cacheType, NSURL *imageURL) {
  123. if (completedBlock) {
  124. completedBlock(image, error, cacheType);
  125. }
  126. }];
  127. }
  128. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(EMSDWebImageCompletedBlock)completedBlock {
  129. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:^(UIImage *image, NSError *error, EMSDImageCacheType cacheType, NSURL *imageURL) {
  130. if (completedBlock) {
  131. completedBlock(image, error, cacheType);
  132. }
  133. }];
  134. }
  135. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(EMSDWebImageOptions)options completed:(EMSDWebImageCompletedBlock)completedBlock {
  136. [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:^(UIImage *image, NSError *error, EMSDImageCacheType cacheType, NSURL *imageURL) {
  137. if (completedBlock) {
  138. completedBlock(image, error, cacheType);
  139. }
  140. }];
  141. }
  142. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(EMSDWebImageOptions)options progress:(EMSDWebImageDownloaderProgressBlock)progressBlock completed:(EMSDWebImageCompletedBlock)completedBlock {
  143. [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:progressBlock completed:^(UIImage *image, NSError *error, EMSDImageCacheType cacheType, NSURL *imageURL) {
  144. if (completedBlock) {
  145. completedBlock(image, error, cacheType);
  146. }
  147. }];
  148. }
  149. - (void)cancelCurrentArrayLoad {
  150. [self sd_cancelCurrentAnimationImagesLoad];
  151. }
  152. - (void)cancelCurrentImageLoad {
  153. [self sd_cancelCurrentImageLoad];
  154. }
  155. - (void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs {
  156. [self sd_setAnimationImagesWithURLs:arrayOfURLs];
  157. }
  158. @end