UIButton+WebCache.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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<NSString *, NSURL *> SDStateImageURLDictionary;
  15. static inline NSString * imageURLKeyForState(UIControlState state) {
  16. return [NSString stringWithFormat:@"image_%lu", (unsigned long)state];
  17. }
  18. static inline NSString * backgroundImageURLKeyForState(UIControlState state) {
  19. return [NSString stringWithFormat:@"backgroundImage_%lu", (unsigned long)state];
  20. }
  21. static inline NSString * imageOperationKeyForState(UIControlState state) {
  22. return [NSString stringWithFormat:@"UIButtonImageOperation%lu", (unsigned long)state];
  23. }
  24. static inline NSString * backgroundImageOperationKeyForState(UIControlState state) {
  25. return [NSString stringWithFormat:@"UIButtonBackgroundImageOperation%lu", (unsigned long)state];
  26. }
  27. @implementation UIButton (WebCache)
  28. #pragma mark - Image
  29. - (nullable NSURL *)sd_currentImageURL {
  30. NSURL *url = self.sd_imageURLStorage[imageURLKeyForState(self.state)];
  31. if (!url) {
  32. url = self.sd_imageURLStorage[imageURLKeyForState(UIControlStateNormal)];
  33. }
  34. return url;
  35. }
  36. - (nullable NSURL *)sd_imageURLForState:(UIControlState)state {
  37. return self.sd_imageURLStorage[imageURLKeyForState(state)];
  38. }
  39. - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state {
  40. [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
  41. }
  42. - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder {
  43. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
  44. }
  45. - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
  46. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
  47. }
  48. - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state completed:(nullable SDExternalCompletionBlock)completedBlock {
  49. [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:completedBlock];
  50. }
  51. - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
  52. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
  53. }
  54. - (void)sd_setImageWithURL:(nullable NSURL *)url
  55. forState:(UIControlState)state
  56. placeholderImage:(nullable UIImage *)placeholder
  57. options:(SDWebImageOptions)options
  58. completed:(nullable SDExternalCompletionBlock)completedBlock {
  59. if (!url) {
  60. [self.sd_imageURLStorage removeObjectForKey:imageURLKeyForState(state)];
  61. } else {
  62. self.sd_imageURLStorage[imageURLKeyForState(state)] = url;
  63. }
  64. __weak typeof(self)weakSelf = self;
  65. [self sd_internalSetImageWithURL:url
  66. placeholderImage:placeholder
  67. options:options
  68. operationKey:imageOperationKeyForState(state)
  69. setImageBlock:^(UIImage *image, NSData *imageData) {
  70. [weakSelf setImage:image forState:state];
  71. }
  72. progress:nil
  73. completed:completedBlock];
  74. }
  75. #pragma mark - Background Image
  76. - (nullable NSURL *)sd_currentBackgroundImageURL {
  77. NSURL *url = self.sd_imageURLStorage[backgroundImageURLKeyForState(self.state)];
  78. if (!url) {
  79. url = self.sd_imageURLStorage[backgroundImageURLKeyForState(UIControlStateNormal)];
  80. }
  81. return url;
  82. }
  83. - (nullable NSURL *)sd_backgroundImageURLForState:(UIControlState)state {
  84. return self.sd_imageURLStorage[backgroundImageURLKeyForState(state)];
  85. }
  86. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state {
  87. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
  88. }
  89. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder {
  90. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
  91. }
  92. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
  93. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
  94. }
  95. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state completed:(nullable SDExternalCompletionBlock)completedBlock {
  96. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:completedBlock];
  97. }
  98. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
  99. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
  100. }
  101. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  102. forState:(UIControlState)state
  103. placeholderImage:(nullable UIImage *)placeholder
  104. options:(SDWebImageOptions)options
  105. completed:(nullable SDExternalCompletionBlock)completedBlock {
  106. if (!url) {
  107. [self.sd_imageURLStorage removeObjectForKey:backgroundImageURLKeyForState(state)];
  108. } else {
  109. self.sd_imageURLStorage[backgroundImageURLKeyForState(state)] = url;
  110. }
  111. __weak typeof(self)weakSelf = self;
  112. [self sd_internalSetImageWithURL:url
  113. placeholderImage:placeholder
  114. options:options
  115. operationKey:backgroundImageOperationKeyForState(state)
  116. setImageBlock:^(UIImage *image, NSData *imageData) {
  117. [weakSelf setBackgroundImage:image forState:state];
  118. }
  119. progress:nil
  120. completed:completedBlock];
  121. }
  122. #pragma mark - Cancel
  123. - (void)sd_cancelImageLoadForState:(UIControlState)state {
  124. [self sd_cancelImageLoadOperationWithKey:imageOperationKeyForState(state)];
  125. }
  126. - (void)sd_cancelBackgroundImageLoadForState:(UIControlState)state {
  127. [self sd_cancelImageLoadOperationWithKey:backgroundImageOperationKeyForState(state)];
  128. }
  129. #pragma mark - Private
  130. - (SDStateImageURLDictionary *)sd_imageURLStorage {
  131. SDStateImageURLDictionary *storage = objc_getAssociatedObject(self, &imageURLStorageKey);
  132. if (!storage) {
  133. storage = [NSMutableDictionary dictionary];
  134. objc_setAssociatedObject(self, &imageURLStorageKey, storage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  135. }
  136. return storage;
  137. }
  138. @end
  139. #endif