forked from gngrwzrd/UIImageLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIImageLoader.h
executable file
·178 lines (128 loc) · 6.64 KB
/
UIImageLoader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#import <TargetConditionals.h>
#if TARGET_OS_IOS || TARGET_OS_TV
#import <UIKit/UIKit.h>
#elif TARGET_OS_OSX
#import <Cocoa/Cocoa.h>
#endif
//https://github.com/gngrwzrd/UIImageLoader
//MARK:- UIImageLoader
// UIImageLoaderImage - typedef for ios/mac compatibility
#if TARGET_OS_IOS || TARGET_OS_TV
typedef UIImage UIImageLoaderImage;
typedef UIActivityIndicatorView UIImageLoaderSpinner;
#elif TARGET_OS_OSX
typedef NSImage UIImageLoaderImage;
typedef NSProgressIndicator UIImageLoaderSpinner;
#endif
//image source passed in completion callbacks.
typedef NS_ENUM(NSInteger,UIImageLoadSource) {
//this is passed to callbacks when there's an error, no image is provided.
UIImageLoadSourceNone, //no image source as there was an error.
//these will be passed to your hasCache callback
UIImageLoadSourceDisk, //image was cached on disk already and loaded from disk
UIImageLoadSourceMemory, //image was in memory cache
//these will be passed to your requestCompleted callback
UIImageLoadSourceNetworkNotModified, //a network request was sent but existing content is still valid
UIImageLoadSourceNetworkToDisk, //a network request was sent, image was updated on disk
};
//forward
@class UIImageMemoryCache;
//block typedefs
typedef void(^UIImageLoader_HasCacheBlock)(UIImageLoaderImage * _Nullable image, UIImageLoadSource loadedFromSource);
typedef void(^UIImageLoader_SendingRequestBlock)(BOOL didHaveCachedImage);
typedef void(^UIImageLoader_RequestCompletedBlock)(NSError * _Nullable error, UIImageLoaderImage * _Nullable image, UIImageLoadSource loadedFromSource);
//error constants
extern NSString * _Nonnull const UIImageLoaderErrorDomain;
extern const NSInteger UIImageLoaderErrorNilURL;
//use the +defaultLoader or create a new one to customize properties.
@interface UIImageLoader : NSObject <NSURLSessionDelegate>
//memory cache where images get stored if cacheImagesInMemory is on.
@property UIImageMemoryCache * _Nullable memoryCache;
//the session object used to download data.
//If you change this then you are responsible for implementing delegate logic for acceptsAnySSLCertificate if needed.
@property (nonatomic) NSURLSession * _Nullable session;
//default location is in home/Library/Caches/my.bundle.id/UIImageLoader
@property (nonatomic) NSURL * _Nonnull cacheDirectory;
//whether to use server cache policy. Default is TRUE
@property BOOL useServerCachePolicy;
//if using server cache control, and the server doesn't return a Cache-Control max-age header, you can use this
//to provide your own max age for caching before the image is requested again.
@property NSTimeInterval defaultCacheControlMaxAge;
//if a response is 4XX, or 5XX. The max tries before a cache control max-age is set.
//The image will be requested again after the max-age for the image. Use this in
//conjunction with badRequestCacheControlMaxAge;
@property NSInteger maxAttemptsForErrors; //default is 3
//the max cache time for error responses.
@property NSInteger defaultCacheControlMaxAgeForErrors; //default is 0 (no cache)
//Whether to trust any ssl certificate. Default is FALSE
@property BOOL trustAnySSLCertificate;
//whether to cache loaded images (from disk) into memory.
@property BOOL cacheImagesInMemory;
//Whether to NSLog image urls when there's a cache miss.
@property BOOL logCacheMisses;
//get the default configured loader.
+ (UIImageLoader * _Nonnull) defaultLoader;
//init with a disk cache url.
- (id _Nullable) initWithCacheDirectory:(NSURL * _Nonnull) url;
//set the Authorization username/password. If set this gets added to every request. Use nil/nil to clear.
- (void) setAuthUsername:(NSString * _Nonnull) username password:(NSString * _Nonnull) password;
//these ignore cache policies and delete files where the modified date is older than specified amount of time.
- (void) clearCachedFilesModifiedOlderThan1Day;
- (void) clearCachedFilesModifiedOlderThan1Week;
- (void) clearCachedFilesModifiedOlderThan:(NSTimeInterval) timeInterval;
//these ignore cache policies and delete files where the created date is older than specified amount of time.
- (void) clearCachedFilesCreatedOlderThan1Day;
- (void) clearCachedFilesCreatedOlderThan1Week;
- (void) clearCachedFilesCreatedOlderThan:(NSTimeInterval) timeInterval;
//ignore cache policy and delete all disk cache files.
- (void) purgeDiskCache;
//purge the memory cache.
- (void) purgeMemoryCache;
//set memory cache max bytes.
- (void) setMemoryCacheMaxBytes:(NSUInteger) maxBytes;
//load an image with URL.
- (NSURLSessionDataTask * _Nullable) loadImageWithURL:(NSURL * _Nullable) url
hasCache:(UIImageLoader_HasCacheBlock _Nullable) hasCache
sendingRequest:(UIImageLoader_SendingRequestBlock _Nullable) sendingRequest
requestCompleted:(UIImageLoader_RequestCompletedBlock _Nullable) requestCompleted;
//load an image with custom request.
//auth headers will be added to your request if needed.
- (NSURLSessionDataTask * _Nullable) loadImageWithRequest:(NSURLRequest * _Nullable) request
hasCache:(UIImageLoader_HasCacheBlock _Nullable) hasCache
sendingRequest:(UIImageLoader_SendingRequestBlock _Nullable) sendingRequest
requestCompleted:(UIImageLoader_RequestCompletedBlock _Nullable) requestCompleted;
@end
//MARK:- UIImageMemoryCache
@interface UIImageMemoryCache : NSObject
//max cache size in bytes.
@property (nonatomic) NSUInteger maxBytes;
//cache an image with URL as key.
- (void) cacheImage:(UIImageLoaderImage * _Nonnull) image forURL:(NSURL * _Nonnull) url;
//remove an image with url as key.
- (void) removeImageForURL:(NSURL * _Nonnull) url;
//delete all cache data.
- (void) purge;
@end
//MARK:- NSImageView & UIImageView additions.
#if TARGET_OS_IOS || TARGET_OS_TV
@interface UIImageView (UIImageLoader)
#elif TARGET_OS_OSX
@interface NSImageView (UIImageLoader)
#endif
#if TARGET_OS_IOS || TARGET_OS_TV
//The views contentMode after the image has loaded.
- (void) uiImageLoader_setFinalContentMode:(UIViewContentMode) finalContentMode;
#elif TARGET_OS_OSX
//The views image scaling value after the image has loaded.
- (void) uiImageLoader_setFinalImageScaling:(NSImageScaling) imageScaling;
#endif
//Whether or not existing running download task should be canceled. You can safely
//ignore this if you want to let images download to be cached.
- (void) uiImageLoader_setCancelsRunningTask:(BOOL) cancelsRunningTask;
//Set a spinner instance. This is retained so you should set it to nil at some point.
- (void) uiImageLoader_setSpinner:(UIImageLoaderSpinner * _Nullable) spinner;
//Set the image with a URL.
- (void) uiImageLoader_setImageWithURL:(NSURL * _Nullable) url;
//Set the image with a URLRequest.
- (void) uiImageLoader_setImageWithRequest:(NSURLRequest * _Nullable) request;
@end