Skip to content

Commit

Permalink
Cancel image request operations in -prepareForReuse, so that new imag…
Browse files Browse the repository at this point in the history
…e loading operations don't depend on previous, now-unnecessary operations
  • Loading branch information
mattt committed Jun 1, 2011
1 parent 71f91db commit 697249a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
3 changes: 2 additions & 1 deletion AFImageRequest.h
Expand Up @@ -34,5 +34,6 @@

+ (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)cancelImageRequestOperationsForURLString:(NSString *)urlString;
+ (void)cancelAllImageRequestOperations;
@end
30 changes: 27 additions & 3 deletions AFImageRequest.m
Expand Up @@ -41,9 +41,9 @@ + (void)requestImageWithURLString:(NSString *)urlString options:(AFImageRequestO
}

+ (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
NSString *cacheKey = [NSString stringWithFormat:@"%fx%f:%d", imageSize.width, imageSize.height, options];
NSURL *url = [NSURL URLWithString:[urlString stringByAppendingString:[NSString stringWithFormat:@"#%@", cacheKey]]];
// Append a hash anchor to the image URL so that unique image options get cached separately
NSString *cacheAnchor = [NSString stringWithFormat:@"%fx%f:%d", imageSize.width, imageSize.height, options];
NSURL *url = [NSURL URLWithString:[urlString stringByAppendingString:[NSString stringWithFormat:@"#%@", cacheAnchor]]];
if (!url) {
if (block) {
block(nil);
Expand All @@ -70,4 +70,28 @@ + (void)requestImageWithURLString:(NSString *)urlString size:(CGSize)imageSize o
[_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
4 changes: 4 additions & 0 deletions AFNetworkingExample/Classes/Views/SpotTableViewCell.m
Expand Up @@ -80,6 +80,10 @@ - (void)setImageURLString:(NSString *)imageURLString options:(AFImageRequestOpti

#pragma mark - UITableViewCell

- (void)prepareForReuse {
[super prepareForReuse];
[AFImageRequest cancelImageRequestOperationsForURLString:self.imageURLString];
}

#pragma mark - UIView

Expand Down

0 comments on commit 697249a

Please sign in to comment.