No Description

UIButton+AFNetworking.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // UIButton+AFNetworking.m
  2. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import "UIButton+AFNetworking.h"
  22. #import <objc/runtime.h>
  23. #if TARGET_OS_IOS || TARGET_OS_TV
  24. #import "UIImageView+AFNetworking.h"
  25. #import "AFImageDownloader.h"
  26. @interface UIButton (_AFNetworking)
  27. @end
  28. @implementation UIButton (_AFNetworking)
  29. #pragma mark -
  30. static char AFImageDownloadReceiptNormal;
  31. static char AFImageDownloadReceiptHighlighted;
  32. static char AFImageDownloadReceiptSelected;
  33. static char AFImageDownloadReceiptDisabled;
  34. static const char * af_imageDownloadReceiptKeyForState(UIControlState state) {
  35. switch (state) {
  36. case UIControlStateHighlighted:
  37. return &AFImageDownloadReceiptHighlighted;
  38. case UIControlStateSelected:
  39. return &AFImageDownloadReceiptSelected;
  40. case UIControlStateDisabled:
  41. return &AFImageDownloadReceiptDisabled;
  42. case UIControlStateNormal:
  43. default:
  44. return &AFImageDownloadReceiptNormal;
  45. }
  46. }
  47. - (AFImageDownloadReceipt *)af_imageDownloadReceiptForState:(UIControlState)state {
  48. return (AFImageDownloadReceipt *)objc_getAssociatedObject(self, af_imageDownloadReceiptKeyForState(state));
  49. }
  50. - (void)af_setImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt
  51. forState:(UIControlState)state
  52. {
  53. objc_setAssociatedObject(self, af_imageDownloadReceiptKeyForState(state), imageDownloadReceipt, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  54. }
  55. #pragma mark -
  56. static char AFBackgroundImageDownloadReceiptNormal;
  57. static char AFBackgroundImageDownloadReceiptHighlighted;
  58. static char AFBackgroundImageDownloadReceiptSelected;
  59. static char AFBackgroundImageDownloadReceiptDisabled;
  60. static const char * af_backgroundImageDownloadReceiptKeyForState(UIControlState state) {
  61. switch (state) {
  62. case UIControlStateHighlighted:
  63. return &AFBackgroundImageDownloadReceiptHighlighted;
  64. case UIControlStateSelected:
  65. return &AFBackgroundImageDownloadReceiptSelected;
  66. case UIControlStateDisabled:
  67. return &AFBackgroundImageDownloadReceiptDisabled;
  68. case UIControlStateNormal:
  69. default:
  70. return &AFBackgroundImageDownloadReceiptNormal;
  71. }
  72. }
  73. - (AFImageDownloadReceipt *)af_backgroundImageDownloadReceiptForState:(UIControlState)state {
  74. return (AFImageDownloadReceipt *)objc_getAssociatedObject(self, af_backgroundImageDownloadReceiptKeyForState(state));
  75. }
  76. - (void)af_setBackgroundImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt
  77. forState:(UIControlState)state
  78. {
  79. objc_setAssociatedObject(self, af_backgroundImageDownloadReceiptKeyForState(state), imageDownloadReceipt, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  80. }
  81. @end
  82. #pragma mark -
  83. @implementation UIButton (AFNetworking)
  84. + (AFImageDownloader *)sharedImageDownloader {
  85. return objc_getAssociatedObject(self, @selector(sharedImageDownloader)) ?: [AFImageDownloader defaultInstance];
  86. }
  87. + (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader {
  88. objc_setAssociatedObject(self, @selector(sharedImageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  89. }
  90. #pragma mark -
  91. - (void)setImageForState:(UIControlState)state
  92. withURL:(NSURL *)url
  93. {
  94. [self setImageForState:state withURL:url placeholderImage:nil];
  95. }
  96. - (void)setImageForState:(UIControlState)state
  97. withURL:(NSURL *)url
  98. placeholderImage:(UIImage *)placeholderImage
  99. {
  100. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  101. [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
  102. [self setImageForState:state withURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
  103. }
  104. - (void)setImageForState:(UIControlState)state
  105. withURLRequest:(NSURLRequest *)urlRequest
  106. placeholderImage:(nullable UIImage *)placeholderImage
  107. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
  108. failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
  109. {
  110. if ([self isActiveTaskURLEqualToURLRequest:urlRequest forState:state]) {
  111. return;
  112. }
  113. [self cancelImageDownloadTaskForState:state];
  114. AFImageDownloader *downloader = [[self class] sharedImageDownloader];
  115. id <AFImageRequestCache> imageCache = downloader.imageCache;
  116. //Use the image from the image cache if it exists
  117. UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil];
  118. if (cachedImage) {
  119. if (success) {
  120. success(urlRequest, nil, cachedImage);
  121. } else {
  122. [self setImage:cachedImage forState:state];
  123. }
  124. [self af_setImageDownloadReceipt:nil forState:state];
  125. } else {
  126. if (placeholderImage) {
  127. [self setImage:placeholderImage forState:state];
  128. }
  129. __weak __typeof(self)weakSelf = self;
  130. NSUUID *downloadID = [NSUUID UUID];
  131. AFImageDownloadReceipt *receipt;
  132. receipt = [downloader
  133. downloadImageForURLRequest:urlRequest
  134. withReceiptID:downloadID
  135. success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
  136. __strong __typeof(weakSelf)strongSelf = weakSelf;
  137. if ([[strongSelf af_imageDownloadReceiptForState:state].receiptID isEqual:downloadID]) {
  138. if (success) {
  139. success(request, response, responseObject);
  140. } else if(responseObject) {
  141. [strongSelf setImage:responseObject forState:state];
  142. }
  143. [strongSelf af_setImageDownloadReceipt:nil forState:state];
  144. }
  145. }
  146. failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
  147. __strong __typeof(weakSelf)strongSelf = weakSelf;
  148. if ([[strongSelf af_imageDownloadReceiptForState:state].receiptID isEqual:downloadID]) {
  149. if (failure) {
  150. failure(request, response, error);
  151. }
  152. [strongSelf af_setImageDownloadReceipt:nil forState:state];
  153. }
  154. }];
  155. [self af_setImageDownloadReceipt:receipt forState:state];
  156. }
  157. }
  158. #pragma mark -
  159. - (void)setBackgroundImageForState:(UIControlState)state
  160. withURL:(NSURL *)url
  161. {
  162. [self setBackgroundImageForState:state withURL:url placeholderImage:nil];
  163. }
  164. - (void)setBackgroundImageForState:(UIControlState)state
  165. withURL:(NSURL *)url
  166. placeholderImage:(nullable UIImage *)placeholderImage
  167. {
  168. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  169. [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
  170. [self setBackgroundImageForState:state withURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
  171. }
  172. - (void)setBackgroundImageForState:(UIControlState)state
  173. withURLRequest:(NSURLRequest *)urlRequest
  174. placeholderImage:(nullable UIImage *)placeholderImage
  175. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
  176. failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
  177. {
  178. if ([self isActiveBackgroundTaskURLEqualToURLRequest:urlRequest forState:state]) {
  179. return;
  180. }
  181. [self cancelBackgroundImageDownloadTaskForState:state];
  182. AFImageDownloader *downloader = [[self class] sharedImageDownloader];
  183. id <AFImageRequestCache> imageCache = downloader.imageCache;
  184. //Use the image from the image cache if it exists
  185. UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil];
  186. if (cachedImage) {
  187. if (success) {
  188. success(urlRequest, nil, cachedImage);
  189. } else {
  190. [self setBackgroundImage:cachedImage forState:state];
  191. }
  192. [self af_setBackgroundImageDownloadReceipt:nil forState:state];
  193. } else {
  194. if (placeholderImage) {
  195. [self setBackgroundImage:placeholderImage forState:state];
  196. }
  197. __weak __typeof(self)weakSelf = self;
  198. NSUUID *downloadID = [NSUUID UUID];
  199. AFImageDownloadReceipt *receipt;
  200. receipt = [downloader
  201. downloadImageForURLRequest:urlRequest
  202. withReceiptID:downloadID
  203. success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
  204. __strong __typeof(weakSelf)strongSelf = weakSelf;
  205. if ([[strongSelf af_backgroundImageDownloadReceiptForState:state].receiptID isEqual:downloadID]) {
  206. if (success) {
  207. success(request, response, responseObject);
  208. } else if(responseObject) {
  209. [strongSelf setBackgroundImage:responseObject forState:state];
  210. }
  211. [strongSelf af_setBackgroundImageDownloadReceipt:nil forState:state];
  212. }
  213. }
  214. failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
  215. __strong __typeof(weakSelf)strongSelf = weakSelf;
  216. if ([[strongSelf af_backgroundImageDownloadReceiptForState:state].receiptID isEqual:downloadID]) {
  217. if (failure) {
  218. failure(request, response, error);
  219. }
  220. [strongSelf af_setBackgroundImageDownloadReceipt:nil forState:state];
  221. }
  222. }];
  223. [self af_setBackgroundImageDownloadReceipt:receipt forState:state];
  224. }
  225. }
  226. #pragma mark -
  227. - (void)cancelImageDownloadTaskForState:(UIControlState)state {
  228. AFImageDownloadReceipt *receipt = [self af_imageDownloadReceiptForState:state];
  229. if (receipt != nil) {
  230. [[self.class sharedImageDownloader] cancelTaskForImageDownloadReceipt:receipt];
  231. [self af_setImageDownloadReceipt:nil forState:state];
  232. }
  233. }
  234. - (void)cancelBackgroundImageDownloadTaskForState:(UIControlState)state {
  235. AFImageDownloadReceipt *receipt = [self af_backgroundImageDownloadReceiptForState:state];
  236. if (receipt != nil) {
  237. [[self.class sharedImageDownloader] cancelTaskForImageDownloadReceipt:receipt];
  238. [self af_setBackgroundImageDownloadReceipt:nil forState:state];
  239. }
  240. }
  241. - (BOOL)isActiveTaskURLEqualToURLRequest:(NSURLRequest *)urlRequest forState:(UIControlState)state {
  242. AFImageDownloadReceipt *receipt = [self af_imageDownloadReceiptForState:state];
  243. return [receipt.task.originalRequest.URL.absoluteString isEqualToString:urlRequest.URL.absoluteString];
  244. }
  245. - (BOOL)isActiveBackgroundTaskURLEqualToURLRequest:(NSURLRequest *)urlRequest forState:(UIControlState)state {
  246. AFImageDownloadReceipt *receipt = [self af_backgroundImageDownloadReceiptForState:state];
  247. return [receipt.task.originalRequest.URL.absoluteString isEqualToString:urlRequest.URL.absoluteString];
  248. }
  249. @end
  250. #endif