UIButton+WebCache.m 12 KB

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