猎豆优选

UIButton+AFNetworking.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #pragma clang diagnostic push
  86. #pragma clang diagnostic ignored "-Wgnu"
  87. return objc_getAssociatedObject(self, @selector(sharedImageDownloader)) ?: [AFImageDownloader defaultInstance];
  88. #pragma clang diagnostic pop
  89. }
  90. + (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader {
  91. objc_setAssociatedObject(self, @selector(sharedImageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  92. }
  93. #pragma mark -
  94. - (void)setImageForState:(UIControlState)state
  95. withURL:(NSURL *)url
  96. {
  97. [self setImageForState:state withURL:url placeholderImage:nil];
  98. }
  99. - (void)setImageForState:(UIControlState)state
  100. withURL:(NSURL *)url
  101. placeholderImage:(UIImage *)placeholderImage
  102. {
  103. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  104. [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
  105. [self setImageForState:state withURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
  106. }
  107. - (void)setImageForState:(UIControlState)state
  108. withURLRequest:(NSURLRequest *)urlRequest
  109. placeholderImage:(nullable UIImage *)placeholderImage
  110. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
  111. failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
  112. {
  113. if ([self isActiveTaskURLEqualToURLRequest:urlRequest forState:state]) {
  114. return;
  115. }
  116. [self cancelImageDownloadTaskForState:state];
  117. AFImageDownloader *downloader = [[self class] sharedImageDownloader];
  118. id <AFImageRequestCache> imageCache = downloader.imageCache;
  119. //Use the image from the image cache if it exists
  120. UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil];
  121. if (cachedImage) {
  122. if (success) {
  123. success(urlRequest, nil, cachedImage);
  124. } else {
  125. [self setImage:cachedImage forState:state];
  126. }
  127. [self af_setImageDownloadReceipt:nil forState:state];
  128. } else {
  129. if (placeholderImage) {
  130. [self setImage:placeholderImage forState:state];
  131. }
  132. __weak __typeof(self)weakSelf = self;
  133. NSUUID *downloadID = [NSUUID UUID];
  134. AFImageDownloadReceipt *receipt;
  135. receipt = [downloader
  136. downloadImageForURLRequest:urlRequest
  137. withReceiptID:downloadID
  138. success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
  139. __strong __typeof(weakSelf)strongSelf = weakSelf;
  140. if ([[strongSelf af_imageDownloadReceiptForState:state].receiptID isEqual:downloadID]) {
  141. if (success) {
  142. success(request, response, responseObject);
  143. } else if(responseObject) {
  144. [strongSelf setImage:responseObject forState:state];
  145. }
  146. [strongSelf af_setImageDownloadReceipt:nil forState:state];
  147. }
  148. }
  149. failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
  150. __strong __typeof(weakSelf)strongSelf = weakSelf;
  151. if ([[strongSelf af_imageDownloadReceiptForState:state].receiptID isEqual:downloadID]) {
  152. if (failure) {
  153. failure(request, response, error);
  154. }
  155. [strongSelf af_setImageDownloadReceipt:nil forState:state];
  156. }
  157. }];
  158. [self af_setImageDownloadReceipt:receipt forState:state];
  159. }
  160. }
  161. #pragma mark -
  162. - (void)setBackgroundImageForState:(UIControlState)state
  163. withURL:(NSURL *)url
  164. {
  165. [self setBackgroundImageForState:state withURL:url placeholderImage:nil];
  166. }
  167. - (void)setBackgroundImageForState:(UIControlState)state
  168. withURL:(NSURL *)url
  169. placeholderImage:(nullable UIImage *)placeholderImage
  170. {
  171. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  172. [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
  173. [self setBackgroundImageForState:state withURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
  174. }
  175. - (void)setBackgroundImageForState:(UIControlState)state
  176. withURLRequest:(NSURLRequest *)urlRequest
  177. placeholderImage:(nullable UIImage *)placeholderImage
  178. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
  179. failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
  180. {
  181. if ([self isActiveBackgroundTaskURLEqualToURLRequest:urlRequest forState:state]) {
  182. return;
  183. }
  184. [self cancelBackgroundImageDownloadTaskForState:state];
  185. AFImageDownloader *downloader = [[self class] sharedImageDownloader];
  186. id <AFImageRequestCache> imageCache = downloader.imageCache;
  187. //Use the image from the image cache if it exists
  188. UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil];
  189. if (cachedImage) {
  190. if (success) {
  191. success(urlRequest, nil, cachedImage);
  192. } else {
  193. [self setBackgroundImage:cachedImage forState:state];
  194. }
  195. [self af_setBackgroundImageDownloadReceipt:nil forState:state];
  196. } else {
  197. if (placeholderImage) {
  198. [self setBackgroundImage:placeholderImage forState:state];
  199. }
  200. __weak __typeof(self)weakSelf = self;
  201. NSUUID *downloadID = [NSUUID UUID];
  202. AFImageDownloadReceipt *receipt;
  203. receipt = [downloader
  204. downloadImageForURLRequest:urlRequest
  205. withReceiptID:downloadID
  206. success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
  207. __strong __typeof(weakSelf)strongSelf = weakSelf;
  208. if ([[strongSelf af_backgroundImageDownloadReceiptForState:state].receiptID isEqual:downloadID]) {
  209. if (success) {
  210. success(request, response, responseObject);
  211. } else if(responseObject) {
  212. [strongSelf setBackgroundImage:responseObject forState:state];
  213. }
  214. [strongSelf af_setBackgroundImageDownloadReceipt:nil forState:state];
  215. }
  216. }
  217. failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
  218. __strong __typeof(weakSelf)strongSelf = weakSelf;
  219. if ([[strongSelf af_backgroundImageDownloadReceiptForState:state].receiptID isEqual:downloadID]) {
  220. if (failure) {
  221. failure(request, response, error);
  222. }
  223. [strongSelf af_setBackgroundImageDownloadReceipt:nil forState:state];
  224. }
  225. }];
  226. [self af_setBackgroundImageDownloadReceipt:receipt forState:state];
  227. }
  228. }
  229. #pragma mark -
  230. - (void)cancelImageDownloadTaskForState:(UIControlState)state {
  231. AFImageDownloadReceipt *receipt = [self af_imageDownloadReceiptForState:state];
  232. if (receipt != nil) {
  233. [[self.class sharedImageDownloader] cancelTaskForImageDownloadReceipt:receipt];
  234. [self af_setImageDownloadReceipt:nil forState:state];
  235. }
  236. }
  237. - (void)cancelBackgroundImageDownloadTaskForState:(UIControlState)state {
  238. AFImageDownloadReceipt *receipt = [self af_backgroundImageDownloadReceiptForState:state];
  239. if (receipt != nil) {
  240. [[self.class sharedImageDownloader] cancelTaskForImageDownloadReceipt:receipt];
  241. [self af_setBackgroundImageDownloadReceipt:nil forState:state];
  242. }
  243. }
  244. - (BOOL)isActiveTaskURLEqualToURLRequest:(NSURLRequest *)urlRequest forState:(UIControlState)state {
  245. AFImageDownloadReceipt *receipt = [self af_imageDownloadReceiptForState:state];
  246. return [receipt.task.originalRequest.URL.absoluteString isEqualToString:urlRequest.URL.absoluteString];
  247. }
  248. - (BOOL)isActiveBackgroundTaskURLEqualToURLRequest:(NSURLRequest *)urlRequest forState:(UIControlState)state {
  249. AFImageDownloadReceipt *receipt = [self af_backgroundImageDownloadReceiptForState:state];
  250. return [receipt.task.originalRequest.URL.absoluteString isEqualToString:urlRequest.URL.absoluteString];
  251. }
  252. @end
  253. #endif