UIImageView+WebCache.m 8.3 KB

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