UNICORN - iOS
HNKCache.h
1 //
2 // HNKCache.h
3 // Haneke
4 //
5 // Created by Hermes Pique on 10/02/14.
6 // Copyright (c) 2014 Hermes Pique. All rights reserved.
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #import <UIKit/UIKit.h>
22 
23 @protocol HNKCacheEntity;
24 @class HNKCacheFormat;
25 
26 #if HANEKE_DEBUG
27 #define HanekeLog(...) NSLog(@"HANEKE: %@", [NSString stringWithFormat:__VA_ARGS__]);
28 #else
29 #define HanekeLog(...)
30 #endif
31 
32 @interface HNKCache : NSObject
33 
34 #pragma mark Initializing the cache
35 
43 - (id)initWithName:(NSString*)name;
44 
49 
56 - (void)registerFormat:(HNKCacheFormat*)format;
57 
61 @property (nonatomic, readonly) NSDictionary *formats;
62 
63 #pragma mark Getting images
64 
76 - (UIImage*)imageForEntity:(id<HNKCacheEntity>)entity formatName:(NSString *)formatName error:(NSError*__autoreleasing *)errorPtr;
77 
85 - (BOOL)retrieveImageForEntity:(id<HNKCacheEntity>)entity formatName:(NSString *)formatName completionBlock:(void(^)(UIImage *image, NSError *error))completionBlock;
86 
94 - (BOOL)retrieveImageForKey:(NSString*)key formatName:(NSString *)formatName completionBlock:(void(^)(UIImage *image, NSError *error))completionBlock;
95 
96 #pragma mark Setting images
97 
109 - (void)setImage:(UIImage*)image forKey:(NSString*)key formatName:(NSString*)formatName;
110 
111 #pragma mark Removing images
112 
119 - (void)removeAllImages;
120 
124 - (void)removeImagesOfFormatNamed:(NSString*)formatName;
125 
129 - (void)removeImagesOfEntity:(id<HNKCacheEntity>)entity;
130 
131 @end
132 
135 @protocol HNKCacheEntity <NSObject>
136 
141 @property (nonatomic, readonly) NSString *cacheKey;
142 
143 @optional
144 
149 @property (nonatomic, readonly) UIImage *cacheOriginalImage;
153 @property (nonatomic, readonly) NSData *cacheOriginalData;
154 
155 
156 @end
157 
158 typedef NS_ENUM(NSInteger, HNKScaleMode)
159 {
160  HNKScaleModeFill = UIViewContentModeScaleToFill,
161  HNKScaleModeAspectFit = UIViewContentModeScaleAspectFit,
162  HNKScaleModeAspectFill = UIViewContentModeScaleAspectFill,
163  HNKScaleModeNone
164 };
165 
166 typedef NS_ENUM(NSInteger, HNKPreloadPolicy)
167 {
168  HNKPreloadPolicyNone,
169  HNKPreloadPolicyLastSession,
170  HNKPreloadPolicyAll
171 };
172 
176 @interface HNKCacheFormat : NSObject
177 
182 @property (nonatomic, assign) BOOL allowUpscaling;
183 
188 @property (nonatomic, assign) CGFloat compressionQuality;
189 
193 @property (nonatomic, readonly) NSString *name;
194 
199 @property (nonatomic, assign) CGSize size;
200 
204 @property (nonatomic, assign) HNKScaleMode scaleMode;
205 
209 @property (nonatomic, assign) unsigned long long diskCapacity;
210 
214 @property (nonatomic, readonly) unsigned long long diskSize;
215 
219 @property (nonatomic, assign) HNKPreloadPolicy preloadPolicy;
220 
226 @property (nonatomic, copy) UIImage* (^preResizeBlock)(NSString *key, UIImage *image);
227 
233 @property (nonatomic, copy) UIImage* (^postResizeBlock)(NSString *key, UIImage *image);
234 
238 - (id)initWithName:(NSString*)name;
239 
245 - (UIImage*)resizedImageFromImage:(UIImage*)image;
246 
247 @end
248 
252 extern NSString *const HNKErrorDomain;
253 
257 extern NSString *const HNKExtendedFileAttributeKey;
258 
259 enum
260 {
261  HNKErrorEntityMustReturnImageOrData = -200,
262  HNKErrorEntityCannotReadImageFromData = -201,
263 
264  HNKErrorDiskCacheMiss = -300,
265  HNKErrorDiskCacheCannotReadFromFile = -301,
266  HNKErrorDiskCacheCannotReadImageFromData = -302
267 };
NSString * name
Definition: HNKCache.h:193
CGFloat compressionQuality
Definition: HNKCache.h:188
UIImage * cacheOriginalImage
Definition: HNKCache.h:149
BOOL allowUpscaling
Definition: HNKCache.h:182
Definition: HNKCache.h:32
NSDictionary * formats
Definition: HNKCache.h:61
NSData * cacheOriginalData
Definition: HNKCache.h:153
HNKPreloadPolicy preloadPolicy
Definition: HNKCache.h:219
NSString * cacheKey
Definition: HNKCache.h:141
Definition: HNKCache.h:135
CGSize size
Definition: HNKCache.h:199
Definition: HNKCache.h:176
void removeAllImages()
Definition: HNKCache.m:363
HNKScaleMode scaleMode
Definition: HNKCache.h:204
unsigned long long diskSize
Definition: HNKCache.h:214
UIImage *(^ postResizeBlock)(NSString *key, UIImage *image)
unsigned long long diskCapacity
Definition: HNKCache.h:209
UIImage *(^ preResizeBlock)(NSString *key, UIImage *image)
HNKCache * sharedCache()
Definition: HNKCache.m:122