|
@@ -20,30 +20,57 @@
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
// THE SOFTWARE.
|
|
|
|
|
|
-#import <Foundation/Foundation.h>
|
|
|
|
|
|
+#import "UIButton+AFNetworking.h"
|
|
|
|
+
|
|
#import <objc/message.h>
|
|
#import <objc/message.h>
|
|
|
|
|
|
|
|
+#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
|
|
|
|
+
|
|
#import "AFHTTPRequestOperation.h"
|
|
#import "AFHTTPRequestOperation.h"
|
|
-#import "AFHTTPSessionManager.h"
|
|
|
|
|
|
|
|
-#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
|
|
|
|
-#import "UIButton+AFNetworking.h"
|
|
|
|
|
|
+static char kAFImageRequestOperationKey;
|
|
|
|
+static char kAFBackgroundImageRequestOperationKey;
|
|
|
|
|
|
-@implementation UIButton (AFNetworking)
|
|
|
|
|
|
+@interface UIButton (_AFNetworking)
|
|
|
|
+@property (readwrite, nonatomic, strong, setter = af_setImageRequestOperation:) AFHTTPRequestOperation *af_imageRequestOperation;
|
|
|
|
+@property (readwrite, nonatomic, strong, setter = af_setBackgroundImageRequestOperation:) AFHTTPRequestOperation *af_backgroundImageRequestOperation;
|
|
|
|
+@end
|
|
|
|
+
|
|
|
|
+@implementation UIButton (_AFNetworking)
|
|
|
|
|
|
-+ (AFHTTPSessionManager *)af_sharedHTTPClient {
|
|
|
|
- static AFHTTPSessionManager *_af_sharedHTTPClient = nil;
|
|
|
|
|
|
++ (NSOperationQueue *)af_sharedImageRequestOperationQueue {
|
|
|
|
+ static NSOperationQueue *_af_sharedImageRequestOperationQueue = nil;
|
|
static dispatch_once_t onceToken;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
dispatch_once(&onceToken, ^{
|
|
- _af_sharedHTTPClient = [[AFHTTPSessionManager alloc] init];
|
|
|
|
- _af_sharedHTTPClient.responseSerializer = [AFImageResponseSerializer serializer];
|
|
|
|
|
|
+ _af_sharedImageRequestOperationQueue = [[NSOperationQueue alloc] init];
|
|
|
|
+ _af_sharedImageRequestOperationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
|
|
});
|
|
});
|
|
|
|
|
|
- return _af_sharedHTTPClient;
|
|
|
|
|
|
+ return _af_sharedImageRequestOperationQueue;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+- (AFHTTPRequestOperation *)af_imageRequestOperation {
|
|
|
|
+ return (AFHTTPRequestOperation *)objc_getAssociatedObject(self, &kAFImageRequestOperationKey);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+- (void)af_setImageRequestOperation:(AFHTTPRequestOperation *)imageRequestOperation {
|
|
|
|
+ objc_setAssociatedObject(self, &kAFImageRequestOperationKey, imageRequestOperation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+- (AFHTTPRequestOperation *)af_backgroundImageRequestOperation {
|
|
|
|
+ return (AFHTTPRequestOperation *)objc_getAssociatedObject(self, &kAFBackgroundImageRequestOperationKey);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+- (void)af_setBackgroundImageRequestOperation:(AFHTTPRequestOperation *)imageRequestOperation {
|
|
|
|
+ objc_setAssociatedObject(self, &kAFBackgroundImageRequestOperationKey, imageRequestOperation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+@end
|
|
|
|
+
|
|
#pragma mark -
|
|
#pragma mark -
|
|
|
|
|
|
|
|
+@implementation UIButton (AFNetworking)
|
|
|
|
+
|
|
- (void)setImageForState:(UIControlState)state
|
|
- (void)setImageForState:(UIControlState)state
|
|
withURL:(NSURL *)url
|
|
withURL:(NSURL *)url
|
|
{
|
|
{
|
|
@@ -66,7 +93,32 @@
|
|
success:(void (^)(NSHTTPURLResponse *response, UIImage *image))success
|
|
success:(void (^)(NSHTTPURLResponse *response, UIImage *image))success
|
|
failure:(void (^)(NSError *error))failure
|
|
failure:(void (^)(NSError *error))failure
|
|
{
|
|
{
|
|
- [self setImageWithSelector:@selector(setImage:forState:) forState:state withURLRequest:urlRequest placeholderImage:placeholderImage success:success failure:failure];
|
|
|
|
|
|
+ [self cancelImageRequestOperation];
|
|
|
|
+
|
|
|
|
+ [self setImage:placeholderImage forState:state];
|
|
|
|
+
|
|
|
|
+ __weak __typeof(self)weakSelf = self;
|
|
|
|
+ self.af_imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
|
|
|
|
+ [self.af_imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
|
|
|
+ __strong __typeof(weakSelf)strongSelf = weakSelf;
|
|
|
|
+ if ([[urlRequest URL] isEqual:[operation.request URL]]) {
|
|
|
|
+ if (success) {
|
|
|
|
+ success(operation.response, responseObject);
|
|
|
|
+ } else if (responseObject) {
|
|
|
|
+ [strongSelf setImage:responseObject forState:state];
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
|
|
+ if ([[urlRequest URL] isEqual:[operation.response URL]]) {
|
|
|
|
+ if (failure) {
|
|
|
|
+ failure(error);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }];
|
|
|
|
+
|
|
|
|
+ [[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_imageRequestOperation];
|
|
}
|
|
}
|
|
|
|
|
|
#pragma mark -
|
|
#pragma mark -
|
|
@@ -93,41 +145,44 @@
|
|
success:(void (^)(NSHTTPURLResponse *response, UIImage *image))success
|
|
success:(void (^)(NSHTTPURLResponse *response, UIImage *image))success
|
|
failure:(void (^)(NSError *error))failure
|
|
failure:(void (^)(NSError *error))failure
|
|
{
|
|
{
|
|
- [self setImageWithSelector:@selector(setBackgroundImage:forState:) forState:state withURLRequest:urlRequest placeholderImage:placeholderImage success:success failure:failure];
|
|
|
|
-}
|
|
|
|
|
|
+ [self cancelBackgroundImageRequestOperation];
|
|
|
|
|
|
-#pragma mark -
|
|
|
|
-
|
|
|
|
-- (void)setImageWithSelector:(SEL)selector
|
|
|
|
- forState:(UIControlState)state
|
|
|
|
- withURLRequest:(NSURLRequest *)urlRequest
|
|
|
|
- placeholderImage:(UIImage *)placeholderImage
|
|
|
|
- success:(void (^)(NSHTTPURLResponse *response, UIImage *image))success
|
|
|
|
- failure:(void (^)(NSError *error))failure
|
|
|
|
-{
|
|
|
|
|
|
+ [self setBackgroundImage:placeholderImage forState:state];
|
|
|
|
|
|
- void (*objc_msgSend_typed)(id, SEL, UIImage *, UIControlState) = (void (*)(id, SEL, UIImage *, UIControlState))objc_msgSend;
|
|
|
|
- objc_msgSend_typed(self, selector, placeholderImage, state);
|
|
|
|
|
|
+ __weak __typeof(self)weakSelf = self;
|
|
|
|
+ self.af_backgroundImageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
|
|
|
|
+ [self.af_backgroundImageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
|
|
|
+ __strong __typeof(weakSelf)strongSelf = weakSelf;
|
|
|
|
+ if ([[urlRequest URL] isEqual:[operation.request URL]]) {
|
|
|
|
+ if (success) {
|
|
|
|
+ success(operation.response, responseObject);
|
|
|
|
+ } else if (responseObject) {
|
|
|
|
+ [strongSelf setBackgroundImage:responseObject forState:state];
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
|
|
- NSURLSessionTask *task = [[[self class] af_sharedHTTPClient] dataTaskWithRequest:urlRequest completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
|
|
|
|
- if (error) {
|
|
|
|
|
|
+ }
|
|
|
|
+ } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
|
|
+ if ([[urlRequest URL] isEqual:[operation.response URL]]) {
|
|
if (failure) {
|
|
if (failure) {
|
|
failure(error);
|
|
failure(error);
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- if (success) {
|
|
|
|
- success((NSHTTPURLResponse *)response, responseObject);
|
|
|
|
- } else if (responseObject) {
|
|
|
|
- objc_msgSend_typed(self, selector, responseObject, state);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}];
|
|
}];
|
|
|
|
|
|
- [task resume];
|
|
|
|
|
|
+ [[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_backgroundImageRequestOperation];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#pragma mark -
|
|
|
|
+
|
|
|
|
+- (void)cancelImageRequestOperation {
|
|
|
|
+ [self.af_imageRequestOperation cancel];
|
|
|
|
+ self.af_imageRequestOperation = nil;
|
|
}
|
|
}
|
|
|
|
|
|
-- (void)cancelImageDataTasks {
|
|
|
|
- [[[[self class] af_sharedHTTPClient] tasks] makeObjectsPerformSelector:@selector(cancel)];
|
|
|
|
|
|
+- (void)cancelBackgroundImageRequestOperation {
|
|
|
|
+ [self.af_backgroundImageRequestOperation cancel];
|
|
|
|
+ self.af_backgroundImageRequestOperation = nil;
|
|
}
|
|
}
|
|
|
|
|
|
@end
|
|
@end
|