Skip to content
Browse files

Adding -cancelImageRequestOperation to UIImageView category

Improving performance of UIImageView -setImageWithURL:... by cancelling running operation if new image url request is different than passed url request
  • Loading branch information...
1 parent 57be1b3 commit 76d9b2bfc8ca7bdde7bbd1dc79d4577817ad3320 @mattt mattt committed Sep 15, 2011
Showing with 10 additions and 5 deletions.
  1. +2 −0 AFNetworking/UIImageView+AFNetworking.h
  2. +8 −5 AFNetworking/UIImageView+AFNetworking.m
View
2 AFNetworking/UIImageView+AFNetworking.h
@@ -41,4 +41,6 @@
options:(AFImageRequestOptions)options
block:(void (^)(UIImage *image, BOOL cacheUsed))block;
+- (void)cancelImageRequestOperation;
+
@end
View
13 AFNetworking/UIImageView+AFNetworking.m
@@ -27,7 +27,7 @@
#import "AFImageCache.h"
-static NSString * const kUIImageViewImageRequestObjectKey = @"imageRequestOperation";
+static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOperation";
@interface UIImageView (_AFNetworking)
@property (readwrite, nonatomic, retain) AFImageRequestOperation *imageRequestOperation;
@@ -86,11 +86,10 @@ - (void)setImageWithURL:(NSURL *)url
options:(AFImageRequestOptions)options
block:(void (^)(UIImage *image, BOOL cacheUsed))block
{
- if (!url) {
- // stop loading image
- [self.imageRequestOperation cancel];
- self.imageRequestOperation = nil;
+ if (!url || [url isEqual:self.imageRequestOperation.request.URL]) {
return;
+ } else {
+ [self cancelImageRequestOperation];
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:30.0];
@@ -125,4 +124,8 @@ - (void)setImageWithURL:(NSURL *)url
}
}
+- (void)cancelImageRequestOperation {
+ [self.imageRequestOperation cancel];
+}
+
@end

0 comments on commit 76d9b2b

Please sign in to comment.
Something went wrong with that request. Please try again.