UIImageView+HighlightedWebCache.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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+HighlightedWebCache.h"
  9. #import "UIView+WebCacheOperation.h"
  10. #define UIImageViewHighlightedWebCacheOperationKey @"highlightedImage"
  11. @implementation UIImageView (HighlightedWebCache)
  12. - (void)sd_setHighlightedImageWithURL:(NSURL *)url {
  13. [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:nil];
  14. }
  15. - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options {
  16. [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:nil];
  17. }
  18. - (void)sd_setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock {
  19. [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:completedBlock];
  20. }
  21. - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock {
  22. [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:completedBlock];
  23. }
  24. - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
  25. [self sd_cancelCurrentHighlightedImageLoad];
  26. if (url) {
  27. __weak UIImageView *wself = self;
  28. id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  29. if (!wself) return;
  30. dispatch_main_sync_safe (^
  31. {
  32. if (!wself) return;
  33. if (image) {
  34. wself.highlightedImage = image;
  35. [wself setNeedsLayout];
  36. }
  37. if (completedBlock && finished) {
  38. completedBlock(image, error, cacheType, url);
  39. }
  40. });
  41. }];
  42. [self sd_setImageLoadOperation:operation forKey:UIImageViewHighlightedWebCacheOperationKey];
  43. } else {
  44. dispatch_main_async_safe(^{
  45. NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
  46. if (completedBlock) {
  47. completedBlock(nil, error, SDImageCacheTypeNone, url);
  48. }
  49. });
  50. }
  51. }
  52. - (void)sd_cancelCurrentHighlightedImageLoad {
  53. [self sd_cancelImageLoadOperationWithKey:UIImageViewHighlightedWebCacheOperationKey];
  54. }
  55. @end
  56. @implementation UIImageView (HighlightedWebCacheDeprecated)
  57. - (void)setHighlightedImageWithURL:(NSURL *)url {
  58. [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:nil];
  59. }
  60. - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options {
  61. [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:nil];
  62. }
  63. - (void)setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock {
  64. [self sd_setHighlightedImageWithURL:url options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  65. if (completedBlock) {
  66. completedBlock(image, error, cacheType);
  67. }
  68. }];
  69. }
  70. - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock {
  71. [self sd_setHighlightedImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  72. if (completedBlock) {
  73. completedBlock(image, error, cacheType);
  74. }
  75. }];
  76. }
  77. - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock {
  78. [self sd_setHighlightedImageWithURL:url options:0 progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  79. if (completedBlock) {
  80. completedBlock(image, error, cacheType);
  81. }
  82. }];
  83. }
  84. - (void)cancelCurrentHighlightedImageLoad {
  85. [self sd_cancelCurrentHighlightedImageLoad];
  86. }
  87. @end