UIImageView+WebCache.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #if SD_UIKIT || SD_MAC
  10. #import "objc/runtime.h"
  11. #import "UIView+WebCacheOperation.h"
  12. #import "UIView+WebCache.h"
  13. @implementation UIImageView (WebCache)
  14. - (void)sd_setImageWithURL:(nullable NSURL *)url {
  15. [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
  16. }
  17. - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder {
  18. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
  19. }
  20. - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
  21. [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
  22. }
  23. - (void)sd_setImageWithURL:(nullable NSURL *)url completed:(nullable SDExternalCompletionBlock)completedBlock {
  24. [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
  25. }
  26. - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
  27. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
  28. }
  29. - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
  30. [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
  31. }
  32. - (void)sd_setImageWithURL:(nullable NSURL *)url
  33. placeholderImage:(nullable UIImage *)placeholder
  34. options:(SDWebImageOptions)options
  35. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  36. completed:(nullable SDExternalCompletionBlock)completedBlock {
  37. [self sd_internalSetImageWithURL:url
  38. placeholderImage:placeholder
  39. options:options
  40. operationKey:nil
  41. setImageBlock:nil
  42. progress:progressBlock
  43. completed:completedBlock];
  44. }
  45. - (void)sd_setImageWithPreviousCachedImageWithURL:(nullable NSURL *)url
  46. placeholderImage:(nullable UIImage *)placeholder
  47. options:(SDWebImageOptions)options
  48. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  49. completed:(nullable SDExternalCompletionBlock)completedBlock {
  50. NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url];
  51. UIImage *lastPreviousCachedImage = [[SDImageCache sharedImageCache] imageFromCacheForKey:key];
  52. [self sd_setImageWithURL:url placeholderImage:lastPreviousCachedImage ?: placeholder options:options progress:progressBlock completed:completedBlock];
  53. }
  54. #if SD_UIKIT
  55. #pragma mark - Animation of multiple images
  56. - (void)sd_setAnimationImagesWithURLs:(nonnull NSArray<NSURL *> *)arrayOfURLs {
  57. [self sd_cancelCurrentAnimationImagesLoad];
  58. __weak __typeof(self)wself = self;
  59. NSMutableArray<id<SDWebImageOperation>> *operationsArray = [[NSMutableArray alloc] init];
  60. [arrayOfURLs enumerateObjectsUsingBlock:^(NSURL *logoImageURL, NSUInteger idx, BOOL * _Nonnull stop) {
  61. id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager loadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  62. if (!wself) return;
  63. dispatch_main_async_safe(^{
  64. __strong UIImageView *sself = wself;
  65. [sself stopAnimating];
  66. if (sself && image) {
  67. NSMutableArray<UIImage *> *currentImages = [[sself animationImages] mutableCopy];
  68. if (!currentImages) {
  69. currentImages = [[NSMutableArray alloc] init];
  70. }
  71. // We know what index objects should be at when they are returned so
  72. // we will put the object at the index, filling any empty indexes
  73. // with the image that was returned too "early". These images will
  74. // be overwritten. (does not require additional sorting datastructure)
  75. while ([currentImages count] < idx) {
  76. [currentImages addObject:image];
  77. }
  78. currentImages[idx] = image;
  79. sself.animationImages = currentImages;
  80. [sself setNeedsLayout];
  81. }
  82. [sself startAnimating];
  83. });
  84. }];
  85. [operationsArray addObject:operation];
  86. }];
  87. [self sd_setImageLoadOperation:[operationsArray copy] forKey:@"UIImageViewAnimationImages"];
  88. }
  89. - (void)sd_cancelCurrentAnimationImagesLoad {
  90. [self sd_cancelImageLoadOperationWithKey:@"UIImageViewAnimationImages"];
  91. }
  92. #endif
  93. @end
  94. #endif