UIButton+WebCache.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #import "objc/runtime.h"
  10. #import "UIView+WebCacheOperation.h"
  11. static char imageURLStorageKey;
  12. @implementation UIButton (WebCache)
  13. - (NSURL *)sd_currentImageURL {
  14. NSURL *url = self.imageURLStorage[@(self.state)];
  15. if (!url) {
  16. url = self.imageURLStorage[@(UIControlStateNormal)];
  17. }
  18. return url;
  19. }
  20. - (NSURL *)sd_imageURLForState:(UIControlState)state {
  21. return self.imageURLStorage[@(state)];
  22. }
  23. - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state {
  24. [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
  25. }
  26. - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder {
  27. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
  28. }
  29. - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
  30. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
  31. }
  32. - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state completed:(SDWebImageCompletionBlock)completedBlock {
  33. [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:completedBlock];
  34. }
  35. - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock {
  36. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
  37. }
  38. - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock {
  39. [self setImage:placeholder forState:state];
  40. [self sd_cancelImageLoadForState:state];
  41. if (!url) {
  42. [self.imageURLStorage removeObjectForKey:@(state)];
  43. dispatch_main_async_safe(^{
  44. NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
  45. if (completedBlock) {
  46. completedBlock(nil, error, SDImageCacheTypeNone, url);
  47. }
  48. });
  49. return;
  50. }
  51. self.imageURLStorage[@(state)] = url;
  52. __weak UIButton *wself = self;
  53. id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  54. if (!wself) return;
  55. dispatch_main_sync_safe(^{
  56. __strong UIButton *sself = wself;
  57. if (!sself) return;
  58. if (image) {
  59. [sself setImage:image forState:state];
  60. }
  61. if (completedBlock && finished) {
  62. completedBlock(image, error, cacheType, url);
  63. }
  64. });
  65. }];
  66. [self sd_setImageLoadOperation:operation forState:state];
  67. }
  68. - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state {
  69. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
  70. }
  71. - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder {
  72. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
  73. }
  74. - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
  75. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
  76. }
  77. - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state completed:(SDWebImageCompletionBlock)completedBlock {
  78. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:completedBlock];
  79. }
  80. - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock {
  81. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
  82. }
  83. - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock {
  84. [self sd_cancelImageLoadForState:state];
  85. [self setBackgroundImage:placeholder forState:state];
  86. if (url) {
  87. __weak UIButton *wself = self;
  88. id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  89. if (!wself) return;
  90. dispatch_main_sync_safe(^{
  91. __strong UIButton *sself = wself;
  92. if (!sself) return;
  93. if (image) {
  94. [sself setBackgroundImage:image forState:state];
  95. }
  96. if (completedBlock && finished) {
  97. completedBlock(image, error, cacheType, url);
  98. }
  99. });
  100. }];
  101. [self sd_setBackgroundImageLoadOperation:operation forState:state];
  102. } else {
  103. dispatch_main_async_safe(^{
  104. NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
  105. if (completedBlock) {
  106. completedBlock(nil, error, SDImageCacheTypeNone, url);
  107. }
  108. });
  109. }
  110. }
  111. - (void)sd_setImageLoadOperation:(id<SDWebImageOperation>)operation forState:(UIControlState)state {
  112. [self sd_setImageLoadOperation:operation forKey:[NSString stringWithFormat:@"UIButtonImageOperation%@", @(state)]];
  113. }
  114. - (void)sd_cancelImageLoadForState:(UIControlState)state {
  115. [self sd_cancelImageLoadOperationWithKey:[NSString stringWithFormat:@"UIButtonImageOperation%@", @(state)]];
  116. }
  117. - (void)sd_setBackgroundImageLoadOperation:(id<SDWebImageOperation>)operation forState:(UIControlState)state {
  118. [self sd_setImageLoadOperation:operation forKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]];
  119. }
  120. - (void)sd_cancelBackgroundImageLoadForState:(UIControlState)state {
  121. [self sd_cancelImageLoadOperationWithKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]];
  122. }
  123. - (NSMutableDictionary *)imageURLStorage {
  124. NSMutableDictionary *storage = objc_getAssociatedObject(self, &imageURLStorageKey);
  125. if (!storage)
  126. {
  127. storage = [NSMutableDictionary dictionary];
  128. objc_setAssociatedObject(self, &imageURLStorageKey, storage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  129. }
  130. return storage;
  131. }
  132. @end
  133. @implementation UIButton (WebCacheDeprecated)
  134. - (NSURL *)currentImageURL {
  135. return [self sd_currentImageURL];
  136. }
  137. - (NSURL *)imageURLForState:(UIControlState)state {
  138. return [self sd_imageURLForState:state];
  139. }
  140. - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state {
  141. [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
  142. }
  143. - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder {
  144. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
  145. }
  146. - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
  147. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
  148. }
  149. - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state completed:(SDWebImageCompletedBlock)completedBlock {
  150. [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  151. if (completedBlock) {
  152. completedBlock(image, error, cacheType);
  153. }
  154. }];
  155. }
  156. - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock {
  157. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  158. if (completedBlock) {
  159. completedBlock(image, error, cacheType);
  160. }
  161. }];
  162. }
  163. - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock {
  164. [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  165. if (completedBlock) {
  166. completedBlock(image, error, cacheType);
  167. }
  168. }];
  169. }
  170. - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state {
  171. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
  172. }
  173. - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder {
  174. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
  175. }
  176. - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
  177. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
  178. }
  179. - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state completed:(SDWebImageCompletedBlock)completedBlock {
  180. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  181. if (completedBlock) {
  182. completedBlock(image, error, cacheType);
  183. }
  184. }];
  185. }
  186. - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock {
  187. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  188. if (completedBlock) {
  189. completedBlock(image, error, cacheType);
  190. }
  191. }];
  192. }
  193. - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock {
  194. [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:options completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  195. if (completedBlock) {
  196. completedBlock(image, error, cacheType);
  197. }
  198. }];
  199. }
  200. - (void)cancelCurrentImageLoad {
  201. // in a backwards compatible manner, cancel for current state
  202. [self sd_cancelImageLoadForState:self.state];
  203. }
  204. - (void)cancelBackgroundImageLoadForState:(UIControlState)state {
  205. [self sd_cancelBackgroundImageLoadForState:state];
  206. }
  207. @end