Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions FastImageCache/FICEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "FICImports.h"
@class FICImageFormat;

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

Expand Down Expand Up @@ -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
4 changes: 1 addition & 3 deletions FastImageCache/FICImageCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
*/
@protocol FICImageCacheDelegate <NSObject>

@required
@optional

/**
This method is called on the delegate when the image cache needs a source image.
Expand Down Expand Up @@ -257,8 +257,6 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
*/
- (void)imageCache:(FICImageCache *)imageCache wantsSourceImageForEntity:(id <FICEntity>)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.

Expand Down
18 changes: 15 additions & 3 deletions FastImageCache/FICImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ @interface FICImageCache () {
NSMutableDictionary *_requests;
__weak id <FICImageCacheDelegate> _delegate;

BOOL _delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock;
BOOL _delegateImplementsShouldProcessAllFormatsInFamilyForEntity;
BOOL _delegateImplementsErrorDidOccurWithMessage;
BOOL _delegateImplementsCancelImageLoadingForEntityWithFormatName;
Expand All @@ -46,6 +47,7 @@ - (void)setDelegate:(id<FICImageCacheDelegate>)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:)];
Expand Down Expand Up @@ -187,7 +189,7 @@ - (BOOL)_retrieveImageForEntity:(id <FICEntity>)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];

Expand All @@ -200,9 +202,19 @@ - (BOOL)_retrieveImageForEntity:(id <FICEntity>)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);
Expand Down