forked from Cocoanetics/DTCoreText
/
DTLazyImageView.m
276 lines (218 loc) · 6.67 KB
/
DTLazyImageView.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
//
// DTLazyImageView.m
// PagingTextScroller
//
// Created by Oliver Drobnik on 5/20/11.
// Copyright 2011 . All rights reserved.
//
#import "DTLazyImageView.h"
#ifndef DT_USE_THREAD_SAFE_INITIALIZATION
#ifndef DT_USE_THREAD_SAFE_INITIALIZATION_NOT_AVAILABLE
#warning Thread safe initialization is not enabled.
#endif
#endif
static NSCache *_imageCache = nil;
@implementation DTLazyImageView
- (void)dealloc
{
self.image = nil;
[_url release];
[_receivedData release];
[_connection cancel];
[_connection release];
if (_imageSource)
CFRelease(_imageSource), _imageSource = NULL;
[super dealloc];
}
- (void)loadImageAtURL:(NSURL *)url
{
if ([NSThread isMainThread])
{
[self performSelectorInBackground:@selector(loadImageAtURL:) withObject:url];
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[_connection start];
CFRunLoopRun();
[request release];
[pool drain];
}
- (void)didMoveToSuperview
{
[super didMoveToSuperview];
if (!self.image && _url && !_connection && self.superview)
{
UIImage *image = [_imageCache objectForKey:_url];
if (image)
{
self.image = image;
_fullWidth = image.size.width;
_fullHeight = image.size.height;
// for unknown reasons direct notify does not work below iOS 5
[self performSelector:@selector(notify) withObject:nil afterDelay:0.0];
return;
}
[self loadImageAtURL:_url];
}
}
- (void)cancelLoading
{
[_connection cancel];
[_connection release], _connection = nil;
[_receivedData release], _receivedData = nil;
}
#pragma mark Progressive Image
-(CGImageRef)newTransitoryImage:(CGImageRef)partialImg
{
const size_t height = CGImageGetHeight(partialImg);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bmContext = CGBitmapContextCreate(NULL, _fullWidth, _fullHeight, 8, _fullWidth * 4, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
if (!bmContext)
{
// fail creating context
return NULL;
}
CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = _fullWidth, .size.height = height}, partialImg);
CGImageRef goodImageRef = CGBitmapContextCreateImage(bmContext);
CGContextRelease(bmContext);
return goodImageRef;
}
- (void)createAndShowProgressiveImage
{
if (!_imageSource)
{
return;
}
/* For progressive download */
const NSUInteger totalSize = [_receivedData length];
CGImageSourceUpdateData(_imageSource, (CFDataRef)_receivedData, (totalSize == _expectedSize) ? true : false);
if (_fullHeight > 0 && _fullWidth > 0)
{
CGImageRef image = CGImageSourceCreateImageAtIndex(_imageSource, 0, NULL);
if (image)
{
CGImageRef imgTmp = [self newTransitoryImage:image]; // iOS fix to correctly handle JPG see : http://www.cocoabyss.com/mac-os-x/progressive-image-download-imageio/
if (imgTmp)
{
UIImage *image = [[UIImage alloc] initWithCGImage:imgTmp];
[self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
[image release];
CGImageRelease(imgTmp);
}
CGImageRelease(image);
}
}
else
{
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(_imageSource, 0, NULL);
if (properties)
{
CFTypeRef val = CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);
if (val)
CFNumberGetValue(val, kCFNumberFloatType, &_fullHeight);
val = CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);
if (val)
CFNumberGetValue(val, kCFNumberFloatType, &_fullWidth);
CFRelease(properties);
}
}
}
#pragma mark NSURL Loading
- (void)notify
{
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSValue valueWithCGSize:CGSizeMake(_fullWidth, _fullHeight)], @"ImageSize", _url, @"ImageURL", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"DTLazyImageViewDidFinishLoading" object:nil userInfo:userInfo];
}
- (void)completeDownloadWithData:(NSData *)data
{
UIImage *image = [[UIImage alloc] initWithData:data];
self.image = image;
_fullWidth = image.size.width;
_fullHeight = image.size.height;
self.bounds = CGRectMake(0, 0, _fullWidth, _fullHeight);
[self notify];
#ifdef DT_USE_THREAD_SAFE_INITIALIZATION
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
_imageCache = [[NSCache alloc] init];
});
#else
if (!_imageCache)
{
_imageCache = [[NSCache alloc] init];
}
#endif
if (_url)
{
// cache image
[_imageCache setObject:image forKey:_url];
}
[image release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// every time we get an response it might be a forward, so we discard what data we have
[_receivedData release], _receivedData = nil;
// does not fire for local file URLs
if ([response isKindOfClass:[NSHTTPURLResponse class]])
{
NSHTTPURLResponse *httpResponse = (id)response;
if (![[httpResponse MIMEType] hasPrefix:@"image"])
{
[self cancelLoading];
return;
}
}
/* For progressive download */
_fullWidth = _fullHeight = -1.0f;
_expectedSize = [response expectedContentLength];
_receivedData = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_receivedData appendData:data];
if (!CGImageSourceCreateIncremental || !shouldShowProgressiveDownload)
{
// don't show progressive
return;
}
if (!_imageSource)
{
_imageSource = CGImageSourceCreateIncremental(NULL);
}
[self createAndShowProgressiveImage];
}
- (void)removeFromSuperview
{
[super removeFromSuperview];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (_receivedData)
{
[self performSelectorOnMainThread:@selector(completeDownloadWithData:) withObject:_receivedData waitUntilDone:YES];
[_receivedData release], _receivedData = nil;
}
[_connection release], _connection = nil;
/* For progressive download */
if (_imageSource)
CFRelease(_imageSource), _imageSource = NULL;
CFRunLoopStop(CFRunLoopGetCurrent());
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[_connection release], _connection = nil;
[_receivedData release], _receivedData = nil;
/* For progressive download */
if (_imageSource)
CFRelease(_imageSource), _imageSource = NULL;
CFRunLoopStop(CFRunLoopGetCurrent());
}
#pragma mark Properties
@synthesize url = _url;
@synthesize shouldShowProgressiveDownload;
@end