瀏覽代碼

Removing code to try and be clever about cancelling image requests or not for a instances of the same image being requested

Adding HTTP pipelining to image requests
Mattt Thompson 14 年之前
父節點
當前提交
4ac2f407d7
共有 2 個文件被更改,包括 13 次插入21 次删除
  1. 3 5
      AFNetworking/AFImageRequestOperation.m
  2. 10 16
      AFNetworking/UIImageView+AFNetworking.m

+ 3 - 5
AFNetworking/AFImageRequestOperation.m

@@ -62,11 +62,9 @@ static inline CGSize kAFImageRequestRoundedCornerRadii(CGSize imageSize) {
             image = [UIImage imageByRoundingCornersOfImage:image corners:UIRectCornerAllCorners cornerRadii:kAFImageRequestRoundedCornerRadii(image.size)];
         }
         
-        dispatch_async(dispatch_get_main_queue(), ^(void) {
-            if (success) {
-                success(image);
-            }
-        });
+        if (success) {
+            success(image);
+        }
         
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
             [[AFImageCache sharedImageCache] cacheImage:image forRequest:request imageSize:imageSize options:options];

+ 10 - 16
AFNetworking/UIImageView+AFNetworking.m

@@ -24,8 +24,6 @@
 
 #import "AFImageCache.h"
 
-static NSOperationQueue *_operationQueue = nil;
-
 static NSString * const kUIImageViewImageRequestObjectKey = @"imageRequestOperation";
 
 @interface UIImageView (_AFNetworking)
@@ -48,11 +46,15 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"imageRequestOperat
     objc_setAssociatedObject(self, kUIImageViewImageRequestObjectKey, imageRequestOperation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
 }
 
-+ (void)initialize {
-    [super initialize];
++ (NSOperationQueue *)sharedImageRequestOperationQueue {
+    static NSOperationQueue *_imageRequestOperationQueue = nil;
+    
+    if (!_imageRequestOperationQueue) {
+        _imageRequestOperationQueue = [[NSOperationQueue alloc] init];
+        [_imageRequestOperationQueue setMaxConcurrentOperationCount:6];
+    }
     
-	_operationQueue = [[NSOperationQueue alloc] init];
-	[_operationQueue setMaxConcurrentOperationCount:6];
+    return _imageRequestOperationQueue;
 }
  
 - (void)setImageWithURL:(NSURL *)url {
@@ -68,16 +70,9 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"imageRequestOperat
         return;
     }
     
-    if (self.imageRequestOperation && ([self.imageRequestOperation isReady] || [self.imageRequestOperation isExecuting])) {
-        if ([[[self.imageRequestOperation request] URL] isEqual:url]) {
-            return;
-        } else {
-            [self.imageRequestOperation cancel]; 
-        }
-    }
-    
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:30.0];
     [request setHTTPShouldHandleCookies:NO];
+    [request setHTTPShouldUsePipelining:YES];
         
     UIImage *cachedImage = [[AFImageCache sharedImageCache] cachedImageForRequest:request imageSize:imageSize options:options];
     if (cachedImage) {
@@ -93,9 +88,8 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"imageRequestOperat
             }
         }];
         
-        [_operationQueue addOperation:self.imageRequestOperation];
+        [[[self class] sharedImageRequestOperationQueue] addOperation:self.imageRequestOperation];
     }
 }
 
-
 @end