Cancel image request operations in -prepareForReuse, so that new image loading operations don't depend on previous, now-unnecessary operations

This commit is contained in:
Mattt Thompson 2011-06-01 09:16:53 -05:00
parent 71f91db2fa
commit 697249a69f
3 changed files with 33 additions and 4 deletions

View file

@ -34,5 +34,6 @@
+ (void)requestImageWithURLString:(NSString *)urlString options:(AFImageRequestOptions)options block:(void (^)(UIImage *image))block; + (void)requestImageWithURLString:(NSString *)urlString options:(AFImageRequestOptions)options block:(void (^)(UIImage *image))block;
+ (void)requestImageWithURLString:(NSString *)urlString size:(CGSize)imageSize options:(AFImageRequestOptions)options block:(void (^)(UIImage *image))block; + (void)requestImageWithURLString:(NSString *)urlString size:(CGSize)imageSize options:(AFImageRequestOptions)options block:(void (^)(UIImage *image))block;
+ (void)cancelImageRequestOperationsForURLString:(NSString *)urlString;
+ (void)cancelAllImageRequestOperations;
@end @end

View file

@ -41,9 +41,9 @@ static NSMutableSet *_cachedRequests = nil;
} }
+ (void)requestImageWithURLString:(NSString *)urlString size:(CGSize)imageSize options:(AFImageRequestOptions)options block:(void (^)(UIImage *image))block { + (void)requestImageWithURLString:(NSString *)urlString size:(CGSize)imageSize options:(AFImageRequestOptions)options block:(void (^)(UIImage *image))block {
// Append a hash to the image URL so that unique image options get cached separately // Append a hash anchor to the image URL so that unique image options get cached separately
NSString *cacheKey = [NSString stringWithFormat:@"%fx%f:%d", imageSize.width, imageSize.height, options]; NSString *cacheAnchor = [NSString stringWithFormat:@"%fx%f:%d", imageSize.width, imageSize.height, options];
NSURL *url = [NSURL URLWithString:[urlString stringByAppendingString:[NSString stringWithFormat:@"#%@", cacheKey]]]; NSURL *url = [NSURL URLWithString:[urlString stringByAppendingString:[NSString stringWithFormat:@"#%@", cacheAnchor]]];
if (!url) { if (!url) {
if (block) { if (block) {
block(nil); block(nil);
@ -70,4 +70,28 @@ static NSMutableSet *_cachedRequests = nil;
[_operationQueue addOperation:operation]; [_operationQueue addOperation:operation];
} }
+ (void)cancelImageRequestOperationsForURLString:(NSString *)urlString {
if (!urlString) {
return;
}
for (AFImageRequestOperation *operation in [_operationQueue operations]) {
NSString *requestURLString = [[[operation request] URL] absoluteString];
NSRange anchorRange = [requestURLString rangeOfString:@"#" options:NSBackwardsSearch];
if (anchorRange.location != NSNotFound && [[requestURLString substringToIndex:anchorRange.location] isEqualToString:urlString]) {
if (!([operation isExecuting] || [operation isCancelled])) {
[operation cancel];
}
}
}
}
+ (void)cancelAllImageRequestOperations {
for (AFImageRequestOperation *operation in [_operationQueue operations]) {
if (!([operation isExecuting] || [operation isCancelled])) {
[operation cancel];
}
}
}
@end @end

View file

@ -80,6 +80,10 @@
#pragma mark - UITableViewCell #pragma mark - UITableViewCell
- (void)prepareForReuse {
[super prepareForReuse];
[AFImageRequest cancelImageRequestOperationsForURLString:self.imageURLString];
}
#pragma mark - UIView #pragma mark - UIView