diff --git a/FastImageCache/FICEntity.h b/FastImageCache/FICEntity.h index fec135d..04ad9b0 100644 --- a/FastImageCache/FICEntity.h +++ b/FastImageCache/FICEntity.h @@ -7,6 +7,7 @@ // #import "FICImports.h" +@class FICImageFormat; typedef void (^FICEntityImageDrawingBlock)(CGContextRef context, CGSize contextSize); @@ -74,4 +75,12 @@ typedef void (^FICEntityImageDrawingBlock)(CGContextRef context, CGSize contextS */ - (FICEntityImageDrawingBlock)drawingBlockForImage:(UIImage *)image withFormatName:(NSString *)formatName; +@optional +/** + Returns the image for a format + + @param format The image format that identifies which image table is requesting the source image. + */ +- (UIImage *)imageForFormat:(FICImageFormat *)format; + @end diff --git a/FastImageCache/FICImageCache.h b/FastImageCache/FICImageCache.h index f8ae039..f59064a 100644 --- a/FastImageCache/FICImageCache.h +++ b/FastImageCache/FICImageCache.h @@ -227,7 +227,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage); */ @protocol FICImageCacheDelegate -@required +@optional /** This method is called on the delegate when the image cache needs a source image. @@ -257,8 +257,6 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage); */ - (void)imageCache:(FICImageCache *)imageCache wantsSourceImageForEntity:(id )entity withFormatName:(NSString *)formatName completionBlock:(FICImageRequestCompletionBlock)completionBlock; -@optional - /** This method is called on the delegate when the image cache has received an image retrieval cancellation request. diff --git a/FastImageCache/FICImageCache.m b/FastImageCache/FICImageCache.m index 2d8ae98..36f3b7b 100644 --- a/FastImageCache/FICImageCache.m +++ b/FastImageCache/FICImageCache.m @@ -27,6 +27,7 @@ @interface FICImageCache () { NSMutableDictionary *_requests; __weak id _delegate; + BOOL _delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock; BOOL _delegateImplementsShouldProcessAllFormatsInFamilyForEntity; BOOL _delegateImplementsErrorDidOccurWithMessage; BOOL _delegateImplementsCancelImageLoadingForEntityWithFormatName; @@ -46,6 +47,7 @@ - (void)setDelegate:(id)delegate { if (delegate != _delegate) { _delegate = delegate; + _delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock = [_delegate respondsToSelector:@selector(imageCache:wantsSourceImageForEntity:withFormatName:completionBlock:)]; _delegateImplementsShouldProcessAllFormatsInFamilyForEntity = [_delegate respondsToSelector:@selector(imageCache:shouldProcessAllFormatsInFamily:forEntity:)]; _delegateImplementsErrorDidOccurWithMessage = [_delegate respondsToSelector:@selector(imageCache:errorDidOccurWithMessage:)]; _delegateImplementsCancelImageLoadingForEntityWithFormatName = [_delegate respondsToSelector:@selector(imageCache:cancelImageLoadingForEntity:withFormatName:)]; @@ -187,7 +189,7 @@ - (BOOL)_retrieveImageForEntity:(id )entity withFormatName:(NSString } }; - if (image == nil && _delegate != nil) { + if (image == nil) { // No image for this UUID exists in the image table. We'll need to ask the delegate to retrieve the source asset. NSURL *sourceImageURL = [entity sourceImageURLWithFormatName:formatName]; @@ -200,9 +202,19 @@ - (BOOL)_retrieveImageForEntity:(id )entity withFormatName:(NSString [_requests setObject:requestDictionary forKey:sourceImageURL]; _FICAddCompletionBlockForEntity(formatName, requestDictionary, entity, completionBlock); - [_delegate imageCache:self wantsSourceImageForEntity:entity withFormatName:formatName completionBlock:^(UIImage *sourceImage) { + UIImage *image; + if ([entity respondsToSelector:@selector(imageForFormat:)]){ + FICImageFormat *format = [self formatWithName:formatName]; + image = [entity imageForFormat:format]; + } + + if (image){ + [self _imageDidLoad:image forURL:sourceImageURL]; + } else if (_delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock){ + [_delegate imageCache:self wantsSourceImageForEntity:entity withFormatName:formatName completionBlock:^(UIImage *sourceImage) { [self _imageDidLoad:sourceImage forURL:sourceImageURL]; - }]; + }]; + } } else { // We have an existing request dictionary, which means this URL is currently being fetched. _FICAddCompletionBlockForEntity(formatName, requestDictionary, entity, completionBlock);