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
This commit is contained in:
Mattt Thompson 2011-09-15 14:29:29 -05:00
parent 57be1b3b6e
commit 76d9b2bfc8
2 changed files with 10 additions and 5 deletions

View file

@ -41,4 +41,6 @@
options:(AFImageRequestOptions)options
block:(void (^)(UIImage *image, BOOL cacheUsed))block;
- (void)cancelImageRequestOperation;
@end

View file

@ -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 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"imageRequestOperat
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 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"imageRequestOperat
}
}
- (void)cancelImageRequestOperation {
[self.imageRequestOperation cancel];
}
@end