UIButton+WebCache.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "UIButton+WebCache.h"
  9. #if SD_UIKIT
  10. #import "objc/runtime.h"
  11. #import "UIView+WebCacheOperation.h"
  12. #import "UIView+WebCache.h"
  13. static char imageURLStorageKey;
  14. typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary;
  15. @implementation UIButton (WebCache)
  16. - (nullable NSURL *)sd_currentImageURL {
  17. NSURL *url = self.imageURLStorage[@(self.state)];
  18. if (!url) {
  19. url = self.imageURLStorage[@(UIControlStateNormal)];
  20. }
  21. return url;
  22. }
  23. - (nullable NSURL *)sd_imageURLForState:(UIControlState)state {
  24. return self.imageURLStorage[@(state)];
  25. }
  26. #pragma mark - Image
  27. - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state {
  28. [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
  29. }
  30. - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder {
  31. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
  32. }
  33. - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
  34. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
  35. }
  36. - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state completed:(nullable SDExternalCompletionBlock)completedBlock {
  37. [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:completedBlock];
  38. }
  39. - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
  40. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
  41. }
  42. - (void)sd_setImageWithURL:(nullable NSURL *)url
  43. forState:(UIControlState)state
  44. placeholderImage:(nullable UIImage *)placeholder
  45. options:(SDWebImageOptions)options
  46. completed:(nullable SDExternalCompletionBlock)completedBlock {
  47. if (!url) {
  48. [self.imageURLStorage removeObjectForKey:@(state)];
  49. return;
  50. }
  51. self.imageURLStorage[@(state)] = url;
  52. __weak typeof(self)weakSelf = self;
  53. [self sd_internalSetImageWithURL:url
  54. placeholderImage:placeholder
  55. options:options
  56. operationKey:[NSString stringWithFormat:@"UIButtonImageOperation%@", @(state)]
  57. setImageBlock:^(UIImage *image, NSData *imageData) {
  58. [weakSelf setImage:image forState:state];
  59. }
  60. progress:nil
  61. completed:completedBlock];
  62. }
  63. #pragma mark - Background image
  64. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state {
  65. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
  66. }
  67. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder {
  68. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
  69. }
  70. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
  71. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
  72. }
  73. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state completed:(nullable SDExternalCompletionBlock)completedBlock {
  74. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:completedBlock];
  75. }
  76. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
  77. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
  78. }
  79. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  80. forState:(UIControlState)state
  81. placeholderImage:(nullable UIImage *)placeholder
  82. options:(SDWebImageOptions)options
  83. completed:(nullable SDExternalCompletionBlock)completedBlock {
  84. if (!url) {
  85. [self.imageURLStorage removeObjectForKey:@(state)];
  86. return;
  87. }
  88. self.imageURLStorage[@(state)] = url;
  89. __weak typeof(self)weakSelf = self;
  90. [self sd_internalSetImageWithURL:url
  91. placeholderImage:placeholder
  92. options:options
  93. operationKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]
  94. setImageBlock:^(UIImage *image, NSData *imageData) {
  95. [weakSelf setBackgroundImage:image forState:state];
  96. }
  97. progress:nil
  98. completed:completedBlock];
  99. }
  100. - (void)sd_setImageLoadOperation:(id<SDWebImageOperation>)operation forState:(UIControlState)state {
  101. [self sd_setImageLoadOperation:operation forKey:[NSString stringWithFormat:@"UIButtonImageOperation%@", @(state)]];
  102. }
  103. - (void)sd_cancelImageLoadForState:(UIControlState)state {
  104. [self sd_cancelImageLoadOperationWithKey:[NSString stringWithFormat:@"UIButtonImageOperation%@", @(state)]];
  105. }
  106. - (void)sd_setBackgroundImageLoadOperation:(id<SDWebImageOperation>)operation forState:(UIControlState)state {
  107. [self sd_setImageLoadOperation:operation forKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]];
  108. }
  109. - (void)sd_cancelBackgroundImageLoadForState:(UIControlState)state {
  110. [self sd_cancelImageLoadOperationWithKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]];
  111. }
  112. - (SDStateImageURLDictionary *)imageURLStorage {
  113. SDStateImageURLDictionary *storage = objc_getAssociatedObject(self, &imageURLStorageKey);
  114. if (!storage) {
  115. storage = [NSMutableDictionary dictionary];
  116. objc_setAssociatedObject(self, &imageURLStorageKey, storage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  117. }
  118. return storage;
  119. }
  120. @end
  121. #endif