From 94b15ce80475fd4e5c4dc5ac85461a2e5b148547 Mon Sep 17 00:00:00 2001 From: CHEN Xian'an Date: Sat, 3 May 2014 22:37:22 +0800 Subject: [PATCH 1/2] opportunity for entity to provide image, marks imageCache:wantsSourceImageForEntity:withFormatName:completionBlock optional --- FastImageCache/FICEntity.h | 9 +++++++++ FastImageCache/FICImageCache.h | 4 ---- FastImageCache/FICImageCache.m | 18 +++++++++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) 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..8b88405 100644 --- a/FastImageCache/FICImageCache.h +++ b/FastImageCache/FICImageCache.h @@ -227,8 +227,6 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage); */ @protocol FICImageCacheDelegate -@required - /** This method is called on the delegate when the image cache needs a source image. @@ -257,8 +255,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); From a088781d34c777bda8b8ce8f806687d37474afc6 Mon Sep 17 00:00:00 2001 From: CHEN Xian'an Date: Sat, 3 May 2014 22:47:45 +0800 Subject: [PATCH 2/2] oops! optional protocals should be marked using @optional exactly --- FastImageCache/FICImageCache.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/FastImageCache/FICImageCache.h b/FastImageCache/FICImageCache.h index 8b88405..f59064a 100644 --- a/FastImageCache/FICImageCache.h +++ b/FastImageCache/FICImageCache.h @@ -227,6 +227,8 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage); */ @protocol FICImageCacheDelegate +@optional + /** This method is called on the delegate when the image cache needs a source image.