/
NYXProgressiveImageView.m
315 lines (269 loc) · 9.23 KB
/
NYXProgressiveImageView.m
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//
// UIImageView+ProgressiveDisplay.m
// NYXImagesKit
//
// Created by @Nyx0uf on 13/01/12.
// Copyright 2012 Benjamin Godard. All rights reserved.
// www.cococabyss.com
//
#import "NYXProgressiveImageView.h"
#import "NYXImagesHelper.h"
#import <ImageIO/ImageIO.h>
#import <CommonCrypto/CommonDigest.h>
@interface NYXProgressiveImageView()
- (CGImageRef)createTransitoryImage:(CGImageRef)partialImage CF_RETURNS_RETAINED;
- (void) initializeAttributes;
+ (NSString*)cacheDirectoryAddress;
- (NSString*)cachedImageSystemName;
- (void)resetCache;
@end
@implementation NYXProgressiveImageView
{
/// Image download connection
NSURLConnection* _connection;
/// Downloaded data
NSMutableData* _dataTemp;
/// Image source for progressive display
CGImageSourceRef _imageSource;
/// Width of the downloaded image
int _imageWidth;
/// Height of the downloaded image
int _imageHeight;
/// Expected image size
long long _expectedSize;
/// Connection queue
dispatch_queue_t _queue;
// Url
NSURL * _url;
}
@synthesize delegate = _delegate;
@synthesize caching = _caching;
@synthesize cacheTime = _cacheTime;
#pragma mark - Init
-(id)init {
self = [super init];
if (self) {
[self initializeAttributes];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initializeAttributes];
}
return self;
}
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initializeAttributes];
}
return self;
}
-(id)initWithImage:(UIImage *)image {
self = [super initWithImage:image];
if (self) {
[self initializeAttributes];
}
return self;
}
-(id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage {
self = [super initWithImage:image highlightedImage:highlightedImage];
if (self) {
[self initializeAttributes];
}
return self;
}
#pragma mark - Public
-(void)loadImageAtURL:(NSURL*)url
{
_url = url;
if (_caching) {
NSFileManager *fileManager = [NSFileManager defaultManager];
// check if file exists on cache
if ([fileManager fileExistsAtPath:[[NYXProgressiveImageView cacheDirectoryAddress] stringByAppendingPathComponent:[self cachedImageSystemName]]])
{
NSDate *mofificationDate = [[fileManager attributesOfItemAtPath:[[NYXProgressiveImageView cacheDirectoryAddress] stringByAppendingPathComponent:[self cachedImageSystemName]] error:nil] objectForKey:NSFileModificationDate];
// check modification date
if (-[mofificationDate timeIntervalSinceNow] > self.cacheTime) {
// Removes old cache file...
[self resetCache];
} else {
// Loads image from cache without networking
NSData *localImageData = [NSData dataWithContentsOfFile:[[NYXProgressiveImageView cacheDirectoryAddress] stringByAppendingPathComponent:[self cachedImageSystemName]]];
UIImage *localImage = [UIImage imageWithData:localImageData];
self.image = localImage;
return;
}
}
}
_queue = dispatch_queue_create("com.cocoabyss.pdlqueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(_queue, ^{
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0f];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[_connection start];
CFRunLoopRun();
});
}
+ (void)resetImageCache
{
[[NSFileManager defaultManager] removeItemAtPath:[NYXProgressiveImageView cacheDirectoryAddress] error:nil];
}
#pragma mark - NSURLConnectionDelegate
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
_imageSource = CGImageSourceCreateIncremental(NULL);
_imageWidth = _imageHeight = -1;
_expectedSize = [response expectedContentLength];
_dataTemp = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
[_dataTemp appendData:data];
const NSUInteger len = [_dataTemp length];
CGImageSourceUpdateData(_imageSource, (__bridge CFDataRef)_dataTemp, (len == _expectedSize) ? true : false);
if (_imageHeight > 0 && _imageWidth > 0)
{
CGImageRef cgImage = CGImageSourceCreateImageAtIndex(_imageSource, 0, NULL);
if (cgImage)
{
if (NYX_IOS_VERSION_LESS_THAN(@"5.0"))
{
/// iOS 4.x fix to correctly handle JPEG images ( http://www.cocoabyss.com/mac-os-x/progressive-image-download-imageio/ )
CGImageRef imgTmp = [self createTransitoryImage:cgImage];
if (imgTmp)
{
__block UIImage* img = [[UIImage alloc] initWithCGImage:imgTmp];
CGImageRelease(imgTmp);
dispatch_async(dispatch_get_main_queue(), ^{
self.image = img;
});
}
}
else
{
__block UIImage* img = [[UIImage alloc] initWithCGImage:cgImage];
dispatch_async(dispatch_get_main_queue(), ^{
self.image = img;
});
}
CGImageRelease(cgImage);
}
}
else
{
CFDictionaryRef dic = CGImageSourceCopyPropertiesAtIndex(_imageSource, 0, NULL);
if (dic)
{
CFTypeRef val = CFDictionaryGetValue(dic, kCGImagePropertyPixelHeight);
if (val)
CFNumberGetValue(val, kCFNumberIntType, &_imageHeight);
val = CFDictionaryGetValue(dic, kCGImagePropertyPixelWidth);
if (val)
CFNumberGetValue(val, kCFNumberIntType, &_imageWidth);
CFRelease(dic);
}
}
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
if (_dataTemp)
{
UIImage* img = [[UIImage alloc] initWithData:_dataTemp];
dispatch_sync(dispatch_get_main_queue(), ^{
self.image = img;
});
if ([_delegate respondsToSelector:@selector(imageDownloadCompletedWithImage:)])
[_delegate imageDownloadCompletedWithImage:img];
_dataTemp = nil;
}
if (self.caching)
{
// Create cache directory if it doesn't exist
BOOL isDir = YES;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:[NYXProgressiveImageView cacheDirectoryAddress] isDirectory:&isDir]) {
[fileManager createDirectoryAtPath:[NYXProgressiveImageView cacheDirectoryAddress] withIntermediateDirectories:NO attributes:nil error:nil];
}
// Write image cache file
NSError *error;
NSData *cachedImage;
// Is it PNG or JPG/JPEG?
if([[_url absoluteString] rangeOfString: @".png" options: NSCaseInsensitiveSearch].location != NSNotFound)
{
cachedImage = UIImagePNGRepresentation(self.image);
}
else
{
cachedImage = UIImageJPEGRepresentation(self.image, 1.0);
}
@try {
[cachedImage writeToFile:[[NYXProgressiveImageView cacheDirectoryAddress] stringByAppendingPathComponent:[self cachedImageSystemName]] options:NSDataWritingAtomic error:&error];
}
@catch (NSException * e) {
// TODO: error handling
}
}
if (_imageSource)
CFRelease(_imageSource);
_connection = nil;
_url = nil;
dispatch_release(_queue);
CFRunLoopStop(CFRunLoopGetCurrent());
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
if ([_delegate respondsToSelector:@selector(imageDownloadFailedWithData:)])
[_delegate imageDownloadFailedWithData:_dataTemp];
_dataTemp = nil;
if (_imageSource)
CFRelease(_imageSource);
_connection = nil;
dispatch_release(_queue);
CFRunLoopStop(CFRunLoopGetCurrent());
}
#pragma mark - Private
-(CGImageRef)createTransitoryImage:(CGImageRef)partialImage
{
const size_t partialHeight = CGImageGetHeight(partialImage);
CGContextRef bmContext = NYXCreateARGBBitmapContext((size_t)_imageWidth, (size_t)_imageHeight, (size_t)_imageWidth * 4);
if (!bmContext)
return NULL;
CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = _imageWidth, .size.height = partialHeight}, partialImage);
CGImageRef goodImageRef = CGBitmapContextCreateImage(bmContext);
CGContextRelease(bmContext);
return goodImageRef;
}
-(void) initializeAttributes {
_cacheTime = (double)NYX_DEFAULT_CACHE_TIME;
_caching = NO;
}
// caching methods
+ (NSString*)cacheDirectoryAddress
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
return [documentsDirectoryPath stringByAppendingPathComponent:@"NYXProgressiveImageViewCache"];
}
- (void)resetCache
{
[[NSFileManager defaultManager] removeItemAtPath:[[NYXProgressiveImageView cacheDirectoryAddress] stringByAppendingPathComponent:[self cachedImageSystemName]] error:nil];
}
- (NSString*)cachedImageSystemName
{
const char *concat_str = [[_url absoluteString] UTF8String];
if (concat_str == nil)
{
return @"";
}
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(concat_str, strlen(concat_str), result);
NSMutableString *hash = [NSMutableString string];
for (int i = 0; i < 16; i++) {
[hash appendFormat:@"%02X", result[i]];
}
return [hash lowercaseString];
}
@end