UIImageView+WebCache.m 8.4 KB

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