Skip to content

Commit aa1d74e

Browse files
committed
Merge pull request #57 from cxa/master
opportunity for entity to provide source image
2 parents e1266e4 + a088781 commit aa1d74e

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

FastImageCache/FICEntity.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "FICImports.h"
10+
@class FICImageFormat;
1011

1112
typedef void (^FICEntityImageDrawingBlock)(CGContextRef context, CGSize contextSize);
1213

@@ -74,4 +75,12 @@ typedef void (^FICEntityImageDrawingBlock)(CGContextRef context, CGSize contextS
7475
*/
7576
- (FICEntityImageDrawingBlock)drawingBlockForImage:(UIImage *)image withFormatName:(NSString *)formatName;
7677

78+
@optional
79+
/**
80+
Returns the image for a format
81+
82+
@param format The image format that identifies which image table is requesting the source image.
83+
*/
84+
- (UIImage *)imageForFormat:(FICImageFormat *)format;
85+
7786
@end

FastImageCache/FICImageCache.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
227227
*/
228228
@protocol FICImageCacheDelegate <NSObject>
229229

230-
@required
230+
@optional
231231

232232
/**
233233
This method is called on the delegate when the image cache needs a source image.
@@ -257,8 +257,6 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
257257
*/
258258
- (void)imageCache:(FICImageCache *)imageCache wantsSourceImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageRequestCompletionBlock)completionBlock;
259259

260-
@optional
261-
262260
/**
263261
This method is called on the delegate when the image cache has received an image retrieval cancellation request.
264262

FastImageCache/FICImageCache.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ @interface FICImageCache () {
2727
NSMutableDictionary *_requests;
2828
__weak id <FICImageCacheDelegate> _delegate;
2929

30+
BOOL _delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock;
3031
BOOL _delegateImplementsShouldProcessAllFormatsInFamilyForEntity;
3132
BOOL _delegateImplementsErrorDidOccurWithMessage;
3233
BOOL _delegateImplementsCancelImageLoadingForEntityWithFormatName;
@@ -46,6 +47,7 @@ - (void)setDelegate:(id<FICImageCacheDelegate>)delegate {
4647
if (delegate != _delegate) {
4748
_delegate = delegate;
4849

50+
_delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock = [_delegate respondsToSelector:@selector(imageCache:wantsSourceImageForEntity:withFormatName:completionBlock:)];
4951
_delegateImplementsShouldProcessAllFormatsInFamilyForEntity = [_delegate respondsToSelector:@selector(imageCache:shouldProcessAllFormatsInFamily:forEntity:)];
5052
_delegateImplementsErrorDidOccurWithMessage = [_delegate respondsToSelector:@selector(imageCache:errorDidOccurWithMessage:)];
5153
_delegateImplementsCancelImageLoadingForEntityWithFormatName = [_delegate respondsToSelector:@selector(imageCache:cancelImageLoadingForEntity:withFormatName:)];
@@ -187,7 +189,7 @@ - (BOOL)_retrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString
187189
}
188190
};
189191

190-
if (image == nil && _delegate != nil) {
192+
if (image == nil) {
191193
// No image for this UUID exists in the image table. We'll need to ask the delegate to retrieve the source asset.
192194
NSURL *sourceImageURL = [entity sourceImageURLWithFormatName:formatName];
193195

@@ -200,9 +202,19 @@ - (BOOL)_retrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString
200202
[_requests setObject:requestDictionary forKey:sourceImageURL];
201203

202204
_FICAddCompletionBlockForEntity(formatName, requestDictionary, entity, completionBlock);
203-
[_delegate imageCache:self wantsSourceImageForEntity:entity withFormatName:formatName completionBlock:^(UIImage *sourceImage) {
205+
UIImage *image;
206+
if ([entity respondsToSelector:@selector(imageForFormat:)]){
207+
FICImageFormat *format = [self formatWithName:formatName];
208+
image = [entity imageForFormat:format];
209+
}
210+
211+
if (image){
212+
[self _imageDidLoad:image forURL:sourceImageURL];
213+
} else if (_delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock){
214+
[_delegate imageCache:self wantsSourceImageForEntity:entity withFormatName:formatName completionBlock:^(UIImage *sourceImage) {
204215
[self _imageDidLoad:sourceImage forURL:sourceImageURL];
205-
}];
216+
}];
217+
}
206218
} else {
207219
// We have an existing request dictionary, which means this URL is currently being fetched.
208220
_FICAddCompletionBlockForEntity(formatName, requestDictionary, entity, completionBlock);

0 commit comments

Comments
 (0)