UIButton+AFNetworking.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // UIButton+AFNetworking.m
  2. // Copyright (c) 2011–2015 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 defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  24. #import "AFURLResponseSerialization.h"
  25. #import "AFHTTPRequestOperation.h"
  26. #import "UIImageView+AFNetworking.h"
  27. @interface UIButton (_AFNetworking)
  28. @end
  29. @implementation UIButton (_AFNetworking)
  30. + (NSOperationQueue *)af_sharedImageRequestOperationQueue {
  31. static NSOperationQueue *_af_sharedImageRequestOperationQueue = nil;
  32. static dispatch_once_t onceToken;
  33. dispatch_once(&onceToken, ^{
  34. _af_sharedImageRequestOperationQueue = [[NSOperationQueue alloc] init];
  35. _af_sharedImageRequestOperationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
  36. });
  37. return _af_sharedImageRequestOperationQueue;
  38. }
  39. #pragma mark -
  40. static char AFImageRequestOperationNormal;
  41. static char AFImageRequestOperationHighlighted;
  42. static char AFImageRequestOperationSelected;
  43. static char AFImageRequestOperationDisabled;
  44. static const char * af_imageRequestOperationKeyForState(UIControlState state) {
  45. switch (state) {
  46. case UIControlStateHighlighted:
  47. return &AFImageRequestOperationHighlighted;
  48. case UIControlStateSelected:
  49. return &AFImageRequestOperationSelected;
  50. case UIControlStateDisabled:
  51. return &AFImageRequestOperationDisabled;
  52. case UIControlStateNormal:
  53. default:
  54. return &AFImageRequestOperationNormal;
  55. }
  56. }
  57. - (AFHTTPRequestOperation *)af_imageRequestOperationForState:(UIControlState)state {
  58. return (AFHTTPRequestOperation *)objc_getAssociatedObject(self, af_imageRequestOperationKeyForState(state));
  59. }
  60. - (void)af_setImageRequestOperation:(AFHTTPRequestOperation *)imageRequestOperation
  61. forState:(UIControlState)state
  62. {
  63. objc_setAssociatedObject(self, af_imageRequestOperationKeyForState(state), imageRequestOperation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  64. }
  65. #pragma mark -
  66. static char AFBackgroundImageRequestOperationNormal;
  67. static char AFBackgroundImageRequestOperationHighlighted;
  68. static char AFBackgroundImageRequestOperationSelected;
  69. static char AFBackgroundImageRequestOperationDisabled;
  70. static const char * af_backgroundImageRequestOperationKeyForState(UIControlState state) {
  71. switch (state) {
  72. case UIControlStateHighlighted:
  73. return &AFBackgroundImageRequestOperationHighlighted;
  74. case UIControlStateSelected:
  75. return &AFBackgroundImageRequestOperationSelected;
  76. case UIControlStateDisabled:
  77. return &AFBackgroundImageRequestOperationDisabled;
  78. case UIControlStateNormal:
  79. default:
  80. return &AFBackgroundImageRequestOperationNormal;
  81. }
  82. }
  83. - (AFHTTPRequestOperation *)af_backgroundImageRequestOperationForState:(UIControlState)state {
  84. return (AFHTTPRequestOperation *)objc_getAssociatedObject(self, af_backgroundImageRequestOperationKeyForState(state));
  85. }
  86. - (void)af_setBackgroundImageRequestOperation:(AFHTTPRequestOperation *)imageRequestOperation
  87. forState:(UIControlState)state
  88. {
  89. objc_setAssociatedObject(self, af_backgroundImageRequestOperationKeyForState(state), imageRequestOperation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  90. }
  91. @end
  92. #pragma mark -
  93. @implementation UIButton (AFNetworking)
  94. + (id <AFImageCache>)sharedImageCache {
  95. #pragma clang diagnostic push
  96. #pragma clang diagnostic ignored "-Wgnu"
  97. return objc_getAssociatedObject(self, @selector(sharedImageCache)) ?: [UIImageView sharedImageCache];
  98. #pragma clang diagnostic pop
  99. }
  100. + (void)setSharedImageCache:(__nullable id <AFImageCache>)imageCache {
  101. objc_setAssociatedObject(self, @selector(sharedImageCache), imageCache, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  102. }
  103. #pragma mark -
  104. - (id <AFURLResponseSerialization>)imageResponseSerializer {
  105. static id <AFURLResponseSerialization> _af_defaultImageResponseSerializer = nil;
  106. static dispatch_once_t onceToken;
  107. dispatch_once(&onceToken, ^{
  108. _af_defaultImageResponseSerializer = [AFImageResponseSerializer serializer];
  109. });
  110. #pragma clang diagnostic push
  111. #pragma clang diagnostic ignored "-Wgnu"
  112. return objc_getAssociatedObject(self, @selector(imageResponseSerializer)) ?: _af_defaultImageResponseSerializer;
  113. #pragma clang diagnostic pop
  114. }
  115. - (void)setImageResponseSerializer:(id <AFURLResponseSerialization>)serializer {
  116. objc_setAssociatedObject(self, @selector(imageResponseSerializer), serializer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  117. }
  118. #pragma mark -
  119. - (void)setImageForState:(UIControlState)state
  120. withURL:(NSURL *)url
  121. {
  122. [self setImageForState:state withURL:url placeholderImage:nil];
  123. }
  124. - (void)setImageForState:(UIControlState)state
  125. withURL:(NSURL *)url
  126. placeholderImage:(UIImage *)placeholderImage
  127. {
  128. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  129. [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
  130. [self setImageForState:state withURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
  131. }
  132. - (void)setImageForState:(UIControlState)state
  133. withURLRequest:(NSURLRequest *)urlRequest
  134. placeholderImage:(UIImage *)placeholderImage
  135. success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * __nullable response, UIImage *image))success
  136. failure:(void (^)(NSError *error))failure
  137. {
  138. [self cancelImageRequestOperationForState:state];
  139. UIImage *cachedImage = [[[self class] sharedImageCache] cachedImageForRequest:urlRequest];
  140. if (cachedImage) {
  141. if (success) {
  142. success(urlRequest, nil, cachedImage);
  143. } else {
  144. [self setImage:cachedImage forState:state];
  145. }
  146. [self af_setImageRequestOperation:nil forState:state];
  147. } else {
  148. if (placeholderImage) {
  149. [self setImage:placeholderImage forState:state];
  150. }
  151. __weak __typeof(self)weakSelf = self;
  152. AFHTTPRequestOperation *imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
  153. imageRequestOperation.responseSerializer = self.imageResponseSerializer;
  154. [imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  155. __strong __typeof(weakSelf)strongSelf = weakSelf;
  156. if ([[urlRequest URL] isEqual:[operation.request URL]]) {
  157. if (success) {
  158. success(operation.request, operation.response, responseObject);
  159. } else if (responseObject) {
  160. [strongSelf setImage:responseObject forState:state];
  161. }
  162. }
  163. [[[strongSelf class] sharedImageCache] cacheImage:responseObject forRequest:urlRequest];
  164. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  165. if ([[urlRequest URL] isEqual:[operation.request URL]]) {
  166. if (failure) {
  167. failure(error);
  168. }
  169. }
  170. }];
  171. [self af_setImageRequestOperation:imageRequestOperation forState:state];
  172. [[[self class] af_sharedImageRequestOperationQueue] addOperation:imageRequestOperation];
  173. }
  174. }
  175. #pragma mark -
  176. - (void)setBackgroundImageForState:(UIControlState)state
  177. withURL:(NSURL *)url
  178. {
  179. [self setBackgroundImageForState:state withURL:url placeholderImage:nil];
  180. }
  181. - (void)setBackgroundImageForState:(UIControlState)state
  182. withURL:(NSURL *)url
  183. placeholderImage:(UIImage *)placeholderImage
  184. {
  185. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  186. [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
  187. [self setBackgroundImageForState:state withURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
  188. }
  189. - (void)setBackgroundImageForState:(UIControlState)state
  190. withURLRequest:(NSURLRequest *)urlRequest
  191. placeholderImage:(UIImage *)placeholderImage
  192. success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * __nullable response, UIImage *image))success
  193. failure:(void (^)(NSError *error))failure
  194. {
  195. [self cancelBackgroundImageRequestOperationForState:state];
  196. UIImage *cachedImage = [[[self class] sharedImageCache] cachedImageForRequest:urlRequest];
  197. if (cachedImage) {
  198. if (success) {
  199. success(urlRequest, nil, cachedImage);
  200. } else {
  201. [self setBackgroundImage:cachedImage forState:state];
  202. }
  203. [self af_setBackgroundImageRequestOperation:nil forState:state];
  204. } else {
  205. if (placeholderImage) {
  206. [self setBackgroundImage:placeholderImage forState:state];
  207. }
  208. __weak __typeof(self)weakSelf = self;
  209. AFHTTPRequestOperation *backgroundImageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
  210. backgroundImageRequestOperation.responseSerializer = self.imageResponseSerializer;
  211. [backgroundImageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  212. __strong __typeof(weakSelf)strongSelf = weakSelf;
  213. if ([[urlRequest URL] isEqual:[operation.request URL]]) {
  214. if (success) {
  215. success(operation.request, operation.response, responseObject);
  216. } else if (responseObject) {
  217. [strongSelf setBackgroundImage:responseObject forState:state];
  218. }
  219. }
  220. [[[strongSelf class] sharedImageCache] cacheImage:responseObject forRequest:urlRequest];
  221. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  222. if ([[urlRequest URL] isEqual:[operation.request URL]]) {
  223. if (failure) {
  224. failure(error);
  225. }
  226. }
  227. }];
  228. [self af_setBackgroundImageRequestOperation:backgroundImageRequestOperation forState:state];
  229. [[[self class] af_sharedImageRequestOperationQueue] addOperation:backgroundImageRequestOperation];
  230. }
  231. }
  232. #pragma mark -
  233. - (void)cancelImageRequestOperationForState:(UIControlState)state {
  234. [[self af_imageRequestOperationForState:state] cancel];
  235. [self af_setImageRequestOperation:nil forState:state];
  236. }
  237. - (void)cancelBackgroundImageRequestOperationForState:(UIControlState)state {
  238. [[self af_backgroundImageRequestOperationForState:state] cancel];
  239. [self af_setBackgroundImageRequestOperation:nil forState:state];
  240. }
  241. @end
  242. #endif