|
| 1 | +// |
| 2 | +// FICImageCache.h |
| 3 | +// FastImageCache |
| 4 | +// |
| 5 | +// Copyright (c) 2013 Path, Inc. |
| 6 | +// See LICENSE for full license agreement. |
| 7 | +// |
| 8 | + |
| 9 | +#import "FICImports.h" |
| 10 | +#import "FICImageFormat.h" |
| 11 | +#import "FICEntity.h" |
| 12 | + |
| 13 | +@protocol FICEntity; |
| 14 | +@protocol FICImageCacheDelegate; |
| 15 | + |
| 16 | +typedef void (^FICImageCacheCompletionBlock)(id <FICEntity> entity, NSString *formatName, UIImage *image); |
| 17 | +typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage); |
| 18 | + |
| 19 | +/** |
| 20 | + `FICImageCache` is the primary class for managing and interacting with the image cache. Applications using the image cache create one or more `<FICImageFormat>` |
| 21 | + objects. These formats effectively act as logical groupings for image data stored in the image cache. An `<FICImageTable>` object is created for each format defined by |
| 22 | + your application to allow for efficient storage and retrieval of image data. Image data is keyed off of objects conforming to the `<FICEntity>` protocol as well as an |
| 23 | + image format name. |
| 24 | + */ |
| 25 | +@interface FICImageCache : NSObject |
| 26 | + |
| 27 | +///---------------------------- |
| 28 | +/// @name Managing the Delegate |
| 29 | +///---------------------------- |
| 30 | + |
| 31 | +/** |
| 32 | + The delegate of the image cache. |
| 33 | + |
| 34 | + @discussion The delegate is responsible for asynchronously providing the source image for an entity. Optionally, the delegate can require that all formats in a format |
| 35 | + family for a particular entity be processed. Any errors that occur in the image cache are also communicated back to the delegate. |
| 36 | + */ |
| 37 | +@property(nonatomic, assign) id <FICImageCacheDelegate> delegate; |
| 38 | + |
| 39 | +///--------------------------------------- |
| 40 | +/// @name Accessing the Shared Image Cache |
| 41 | +///--------------------------------------- |
| 42 | + |
| 43 | +/** |
| 44 | + Convenience accessor to retrieve a shared image cache instance. |
| 45 | + */ |
| 46 | ++ (instancetype)sharedImageCache; |
| 47 | + |
| 48 | +///--------------------------------- |
| 49 | +/// @name Working with Image Formats |
| 50 | +///--------------------------------- |
| 51 | + |
| 52 | +/** |
| 53 | + Sets the image formats to be used by the image cache. |
| 54 | + |
| 55 | + @param formats An array of `<FICImageFormat>` objects. |
| 56 | + |
| 57 | + @note Once the image formats have been set, subsequent calls to this method will do nothing. |
| 58 | + */ |
| 59 | +- (void)setFormats:(NSArray *)formats; |
| 60 | + |
| 61 | +/** |
| 62 | + Returns an image format previously associated with the image cache. |
| 63 | + |
| 64 | + @param formatName The name of the image format to return. |
| 65 | + |
| 66 | + @return An image format with the name `formatName` or `nil` if no format with that name exists. |
| 67 | + */ |
| 68 | +- (FICImageFormat *)formatWithName:(NSString *)formatName; |
| 69 | + |
| 70 | +/** |
| 71 | + Returns all the image formats of the same family previously associated with the image cache. |
| 72 | + |
| 73 | + @param family The name of the family of image formats to return. |
| 74 | + |
| 75 | + @return An array of `<FICImageFormat>` objects whose family is `family` or `nil` if no format belongs to that family. |
| 76 | + */ |
| 77 | +- (NSArray *)formatsWithFamily:(NSString *)family; |
| 78 | + |
| 79 | +///----------------------------------------------- |
| 80 | +/// @name Storing, Retrieving, and Deleting Images |
| 81 | +///----------------------------------------------- |
| 82 | + |
| 83 | +/** |
| 84 | + Manually sets the the image to be used by the image cache for a particular entity and format name. |
| 85 | + |
| 86 | + @discussion Usually the image cache's delegate is responsible for lazily providing the source image for a given entity. This source image is then processed according |
| 87 | + to the drawing block defined by an entity for a given image format. This method allows the sender to explicitly set the image data to be stored in the image cache. |
| 88 | + After the image has been processed by the image cache, the completion block is called asynchronously on the main queue. |
| 89 | + |
| 90 | + @param image The image to store in the image cache. |
| 91 | + |
| 92 | + @param entity The entity that uniquely identifies the source image. |
| 93 | + |
| 94 | + @param formatName The format name that uniquely identifies which image table to look in for the cached image. |
| 95 | + |
| 96 | + @param completionBlock The completion block that is called after the image has been processed or if an error occurs. |
| 97 | + |
| 98 | + The completion block's type is defined as follows: |
| 99 | + |
| 100 | + typedef void (^FICImageCacheCompletionBlock)(id <FICEntity> entity, NSString *formatName, UIImage *image) |
| 101 | + */ |
| 102 | +- (void)setImage:(UIImage *)image forEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageCacheCompletionBlock)completionBlock; |
| 103 | + |
| 104 | +/** |
| 105 | + Attempts to synchronously retrieve an image from the image cache. |
| 106 | + |
| 107 | + @param entity The entity that uniquely identifies the source image. |
| 108 | + |
| 109 | + @param formatName The format name that uniquely identifies which image table to look in for the cached image. |
| 110 | + |
| 111 | + @param completionBlock The completion block that is called when the requested image is available or if an error occurs. |
| 112 | + |
| 113 | + The completion block's type is defined as follows: |
| 114 | + |
| 115 | + typedef void (^FICImageCacheCompletionBlock)(id <FICEntity> entity, NSString *formatName, UIImage *image) |
| 116 | + |
| 117 | + If the requested image already exists in the image cache, then the completion block is immediately called synchronously on the current thread. If the requested image |
| 118 | + does not already exist in the image cache, then the completion block will be called asynchronously on the main thread as soon as the requested image is available. |
| 119 | + |
| 120 | + @return `YES` if the requested image already exists in the image case, `NO` if the image needs to be provided to the image cache by its delegate. |
| 121 | + |
| 122 | + @discussion Even if you make a synchronous image retrieval request, if the image does not yet exist in the image cache, the delegate will be asked to provide a source |
| 123 | + image, and it will be processed. This always occurs asynchronously. In this case, the return value from this method will be `NO`, and the image will be available in the |
| 124 | + completion block. |
| 125 | + |
| 126 | + @note You can always rely on the completion block being called. If an error occurs for any reason, the `image` parameter of the completion block will be `nil`. See |
| 127 | + <[FICImageCacheDelegate imageCache:errorDidOccurWithMessage:]> for information about being notified when errors occur. |
| 128 | + */ |
| 129 | +- (BOOL)retrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageCacheCompletionBlock)completionBlock; |
| 130 | + |
| 131 | +/** |
| 132 | + Asynchronously retrieves an image from the image cache. |
| 133 | + |
| 134 | + @param entity The entity that uniquely identifies the source image. |
| 135 | + |
| 136 | + @param formatName The format name that uniquely identifies which image table to look in for the cached image. |
| 137 | + |
| 138 | + @param completionBlock The completion block that is called when the requested image is available or if an error occurs. |
| 139 | + |
| 140 | + The completion block's type is defined as follows: |
| 141 | + |
| 142 | + typedef void (^FICImageCacheCompletionBlock)(id <FICEntity> entity, NSString *formatName, UIImage *image) |
| 143 | + |
| 144 | + Unlike its synchronous counterpart, this method will always call its completion block asynchronously on the main thread, even if the request image is already in the |
| 145 | + image cache. |
| 146 | + |
| 147 | + @return `YES` if the requested image already exists in the image case, `NO` if the image needs to be provided to the image cache by its delegate. |
| 148 | + |
| 149 | + @note You can always rely on the completion block being called. If an error occurs for any reason, the `image` parameter of the completion block will be `nil`. See |
| 150 | + <[FICImageCacheDelegate imageCache:errorDidOccurWithMessage:]> for information about being notified when errors occur. |
| 151 | + |
| 152 | + @see [FICImageCache retrieveImageForEntity:withFormatName:completionBlock:] |
| 153 | + */ |
| 154 | +- (BOOL)asynchronouslyRetrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageCacheCompletionBlock)completionBlock; |
| 155 | + |
| 156 | +/** |
| 157 | + Deletes an image from the image cache. |
| 158 | + |
| 159 | + @param entity The entity that uniquely identifies the source image. |
| 160 | + |
| 161 | + @param formatName The format name that uniquely identifies which image table to look in for the cached image. |
| 162 | + */ |
| 163 | +- (void)deleteImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName; |
| 164 | + |
| 165 | +///----------------------------------- |
| 166 | +/// @name Checking for Image Existence |
| 167 | +///----------------------------------- |
| 168 | + |
| 169 | +/** |
| 170 | + Returns whether or not an image exists in the image cache. |
| 171 | + |
| 172 | + @param entity The entity that uniquely identifies the source image. |
| 173 | + |
| 174 | + @param formatName The format name that uniquely identifies which image table to look in for the cached image. |
| 175 | + |
| 176 | + @return `YES` if an image exists in the image cache for a given entity and format name. Otherwise, `NO`. |
| 177 | + */ |
| 178 | +- (BOOL)imageExistsForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName; |
| 179 | + |
| 180 | +///-------------------------------- |
| 181 | +/// @name Resetting the Image Cache |
| 182 | +///-------------------------------- |
| 183 | + |
| 184 | +/** |
| 185 | + Resets the image cache by deleting all image tables and their contents. |
| 186 | + |
| 187 | + @note Resetting an image cache does not reset its image formats. |
| 188 | + */ |
| 189 | +- (void)reset; |
| 190 | + |
| 191 | +@end |
| 192 | + |
| 193 | +/** |
| 194 | + `FICImageCacheDelegate` defines the required and optional actions that an image cache's delegate can perform. |
| 195 | + */ |
| 196 | +@protocol FICImageCacheDelegate <NSObject> |
| 197 | + |
| 198 | +@required |
| 199 | + |
| 200 | +/** |
| 201 | + This method is called on the delegate when the image cache needs a source image. |
| 202 | + |
| 203 | + @param imageCache The image cache that is requesting the source image. |
| 204 | + |
| 205 | + @param entity The entity that uniquely identifies the source image. |
| 206 | + |
| 207 | + @param formatName The format name that uniquely identifies which image table to look in for the cached image. |
| 208 | + |
| 209 | + @param completionBlock The completion block that the receiver must call when it has a source image ready. |
| 210 | + |
| 211 | + The completion block's type is defined as follows: |
| 212 | + |
| 213 | + typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage) |
| 214 | + |
| 215 | + The completion block must always be called on the main thread. |
| 216 | + |
| 217 | + @discussion A source image is usually the original, full-size image that represents an entity. This source image is processed for every unique format to create the |
| 218 | + actual image data to be stored in the image cache. This method is an asynchronous data provider, so nothing is actually returned to the sender. Instead, the delegate's |
| 219 | + implementation is expected to call the completion block once an image is available. |
| 220 | + |
| 221 | + Fast Image Cache is architected under the typical design pattern whereby model objects provide a URL to certain image assets and allow the client to actually retrieve |
| 222 | + the images via network requests only when needed. As a result, the implementation of this method will usually involve creating an asynchronous network request using |
| 223 | + the URL returned by <[FICEntity sourceImageURLWithFormatName:]>, deserializing the image data when the request completes, and finally calling this method's completion |
| 224 | + block to provide the image cache with the source image. |
| 225 | + */ |
| 226 | +- (void)imageCache:(FICImageCache *)imageCache wantsSourceImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageRequestCompletionBlock)completionBlock; |
| 227 | + |
| 228 | +@optional |
| 229 | + |
| 230 | +/** |
| 231 | + This method is called on the delegate to determine whether or not all formats in a family should be processed right now. |
| 232 | + |
| 233 | + @note If this method is not implemented by the delegate, the default value is `YES`. |
| 234 | + |
| 235 | + @param imageCache The image cache that is requesting the source image. |
| 236 | + |
| 237 | + @param formatFamily The name of a format family. |
| 238 | + |
| 239 | + @param entity The entity that uniquely identifies the source image. |
| 240 | + |
| 241 | + @return `YES` if all formats in a format family should be processed. Otherwise, `NO`. |
| 242 | + |
| 243 | + @discussion This method is called whenever new image data is stored in the image cache. Because format families are used to group multiple different formats together, |
| 244 | + typically the delegate will want to return `YES` here so that other formats in the same family can be processed. |
| 245 | + |
| 246 | + For example, if your image cache has defined several different thumbnail sizes and styles for a person model, and if a person changes their profile photo, you would |
| 247 | + want every thumbnail size and style is updated with the new source image. |
| 248 | + */ |
| 249 | +- (BOOL)imageCache:(FICImageCache *)imageCache shouldProcessAllFormatsInFamily:(NSString *)formatFamily forEntity:(id <FICEntity>)entity; |
| 250 | + |
| 251 | +/** |
| 252 | + This method is called on the delegate whenever the image cache has an error message to log. |
| 253 | + |
| 254 | + @param imageCache The image cache that is requesting the source image. |
| 255 | + |
| 256 | + @param errorMessage The error message generated by the image cache. |
| 257 | + |
| 258 | + @discussion Fast Image Cache will not explicitly log any messages to standard output. Instead, it allows the delegate to handle (or ignore) any error output. |
| 259 | + */ |
| 260 | +- (void)imageCache:(FICImageCache *)imageCache errorDidOccurWithMessage:(NSString *)errorMessage; |
| 261 | + |
| 262 | +@end |
0 commit comments