Skip to content

Commit 346e91b

Browse files
author
Mallory Paine
committed
API to cancel loading an image
1 parent f918cd5 commit 346e91b

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

FastImageCache/FICImageCache.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
162162
*/
163163
- (void)deleteImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName;
164164

165+
- (void)cancelImageRetrievalForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName;
166+
165167
///-----------------------------------
166168
/// @name Checking for Image Existence
167169
///-----------------------------------
@@ -227,6 +229,9 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
227229

228230
@optional
229231

232+
- (void)imageCache:(FICImageCache *)imageCache cancelImageLoadingForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName;
233+
234+
230235
/**
231236
This method is called on the delegate to determine whether or not all formats in a family should be processed right now.
232237

FastImageCache/FICImageCache.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ @interface FICImageCache () {
2929

3030
BOOL _delegateImplementsShouldProcessAllFormatsInFamilyForEntity;
3131
BOOL _delegateImplementsErrorDidOccurWithMessage;
32+
BOOL _delegateImplementsCancelImageLoadingForEntityWithFormatName;
3233
}
3334

3435
@end
@@ -47,6 +48,7 @@ - (void)setDelegate:(id<FICImageCacheDelegate>)delegate {
4748

4849
_delegateImplementsShouldProcessAllFormatsInFamilyForEntity = [_delegate respondsToSelector:@selector(imageCache:shouldProcessAllFormatsInFamily:forEntity:)];
4950
_delegateImplementsErrorDidOccurWithMessage = [_delegate respondsToSelector:@selector(imageCache:errorDidOccurWithMessage:)];
51+
_delegateImplementsCancelImageLoadingForEntityWithFormatName = [_delegate respondsToSelector:@selector(imageCache:cancelImageLoadingForEntity:withFormatName:)];
5052
}
5153
}
5254

@@ -369,6 +371,31 @@ - (void)deleteImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)f
369371
[imageTable saveMetadata];
370372
}
371373

374+
- (void)cancelImageRetrievalForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName {
375+
NSURL *sourceImageURL = [entity sourceImageURLWithFormatName:formatName];
376+
NSMutableDictionary *requestDictionary = [_requests objectForKey:sourceImageURL];
377+
if (requestDictionary) {
378+
NSString *entityUUID = [entity UUID];
379+
NSMutableDictionary *entityRequestsDictionary = [requestDictionary objectForKey:entityUUID];
380+
if (entityRequestsDictionary) {
381+
NSMutableDictionary *completionBlocksDictionary = [entityRequestsDictionary objectForKey:FICImageCacheCompletionBlocksKey];
382+
[completionBlocksDictionary removeObjectForKey:formatName];
383+
384+
if ([completionBlocksDictionary count] == 0) {
385+
[requestDictionary removeObjectForKey:entityUUID];
386+
}
387+
388+
if ([requestDictionary count] == 0) {
389+
[_requests removeObjectForKey:sourceImageURL];
390+
391+
if (_delegateImplementsCancelImageLoadingForEntityWithFormatName) {
392+
[_delegate imageCache:self cancelImageLoadingForEntity:entity withFormatName:formatName];
393+
}
394+
}
395+
}
396+
}
397+
}
398+
372399
- (void)reset {
373400
for (FICImageTable *imageTable in [_imageTables allValues]) {
374401
[imageTable reset];

0 commit comments

Comments
 (0)