forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageLoader.m
877 lines (742 loc) · 23.4 KB
/
ImageLoader.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "ImageLoader.h"
#import "OperationQueue.h"
#import "TiUtils.h"
#import "ASIHTTPRequest.h"
#import "TiApp.h"
#import "UIImage+Resize.h"
#import <CommonCrypto/CommonDigest.h>
//#define DEBUG_IMAGE_CACHE
#ifdef DEBUG_IMAGE_CACHE
#import <mach/mach.h>
#endif
@protocol TiResizableImageIOS6Support <NSObject>
@optional
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(NSInteger)resizingMode;
@end
@interface ImageCacheEntry : NSObject
{
UIImage * fullImage;
UIImage * stretchableImage;
UIImage * recentlyResizedImage;
TiDimension leftCap;
TiDimension topCap;
BOOL recapStretchableImage;
BOOL hires;
NSString* localPath;
NSURL* remoteURL;
NSDate* lastModified;
BOOL local;
}
@property(nonatomic,readonly) NSString * localPath;
@property(nonatomic,readwrite,retain) UIImage * fullImage;
@property(nonatomic,readwrite,retain) UIImage* recentlyResizedImage;
@property(nonatomic,readonly) UIImage* stretchableImage;
@property(nonatomic,readwrite) TiDimension leftCap;
@property(nonatomic,readwrite) TiDimension topCap;
@property(nonatomic,readwrite) BOOL hires;
@property(nonatomic,readonly) NSDate* lastModified;
@property(nonatomic,readonly) BOOL local;
-(ImageCacheEntry*)initWithURL:(NSURL*)url;
-(UIImage *)imageForSize:(CGSize)imageSize;
-(void)setData:(NSData*)data;
-(void)serialize:(NSData*)data;
+(NSString*)cachePathForURL:(NSURL*)url;
@end
@implementation ImageCacheEntry
@synthesize fullImage, leftCap, topCap, hires, localPath, stretchableImage, recentlyResizedImage, lastModified, local;
- (UIImage *)fullImage {
if(fullImage == nil) {
if(localPath == nil) {
return nil;
}
#ifdef DEBUG_IMAGE_CACHE
NSLog(@"[CACHE DEBUG] Retrieving local image [lazy]: %@", localPath);
#endif
RELEASE_TO_NIL(stretchableImage);
RELEASE_TO_NIL(recentlyResizedImage);
RELEASE_TO_NIL(lastModified);
fullImage = [[UIImage alloc] initWithContentsOfFile:localPath];
if (local) {
lastModified = [[[[NSFileManager defaultManager] attributesOfItemAtPath:localPath error:nil] objectForKey:NSFileModificationDate] retain];
}
}
return fullImage;
}
- (void)setData:(NSData *)data
{
RELEASE_TO_NIL(fullImage);
RELEASE_TO_NIL(stretchableImage);
RELEASE_TO_NIL(recentlyResizedImage);
RELEASE_TO_NIL(lastModified);
fullImage = [[UIImage alloc] initWithData:data];
[self serialize:data];
}
-(void)setLeftCap:(TiDimension)cap
{
if (!TiDimensionEqual(leftCap, cap)) {
leftCap = cap;
recapStretchableImage = YES;
}
}
-(void)setTopCap:(TiDimension)cap
{
if (!TiDimensionEqual(topCap, cap)) {
topCap = cap;
recapStretchableImage = YES;
}
}
-(UIImage *)stretchableImage
{
if (stretchableImage == nil || recapStretchableImage) {
[stretchableImage release];
UIImage *theImage = [self fullImage];
CGFloat maxWidth = [theImage size].width;
CGFloat maxHeight = [theImage size].height;
NSInteger left = (TiDimensionIsAuto(leftCap) || TiDimensionIsUndefined(leftCap) || leftCap.value == 0) ?
maxWidth/2 :
TiDimensionCalculateValue(leftCap, maxWidth);
NSInteger top = (TiDimensionIsAuto(topCap) || TiDimensionIsUndefined(topCap) || topCap.value == 0) ?
maxHeight/2 :
TiDimensionCalculateValue(topCap, maxHeight);
if ([TiUtils isIOS5OrGreater]) {
if (left >= maxWidth) {
left = maxWidth - 2;
}
if (top >= maxHeight) {
top = maxHeight - 2;
}
NSInteger right = left;
NSInteger bottom = top;
if ((left + right) >= maxWidth) {
right = maxWidth - (left + 1);
}
if ((top + bottom) >= maxHeight) {
bottom = maxHeight - (top + 1);
}
if ([theImage respondsToSelector:@selector(resizableImageWithCapInsets:resizingMode:)]) {
//1 = UIImageResizingModeStretch
stretchableImage = [[theImage resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:1] retain];
}
else {
stretchableImage = [[theImage resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)] retain];
}
}
else
{
if (left >= maxWidth) {
left = maxWidth - 2;
}
if (top > maxHeight) {
top = maxHeight - 2;
}
stretchableImage = [[theImage stretchableImageWithLeftCapWidth:left
topCapHeight:top] retain];
}
recapStretchableImage = NO;
}
return stretchableImage;
}
-(UIImage *)imageForSize:(CGSize)imageSize scalingStyle:(TiImageScalingStyle)scalingStyle
{
if (scalingStyle == TiImageScalingStretch)
{
return [self stretchableImage];
}
CGSize fullImageSize = [[self fullImage] size];
if (scalingStyle != TiImageScalingNonProportional)
{
BOOL validScale = NO;
CGFloat scale = 1.0;
if (imageSize.height > 1.0)
{
scale = imageSize.height/fullImageSize.height;
validScale = YES;
}
if (imageSize.width > 1.0)
{
CGFloat widthScale = imageSize.width/fullImageSize.width;
if (!validScale || (widthScale<scale))
{
scale = widthScale;
validScale = YES;
}
}
if (validScale && ((scalingStyle != TiImageScalingThumbnail) || (scale < 1.0)))
{
imageSize = CGSizeMake(ceilf(scale*fullImageSize.width),
ceilf(scale*fullImageSize.height));
}
else
{
imageSize = fullImageSize;
}
}
if (CGSizeEqualToSize(imageSize, fullImageSize))
{
return fullImage;
}
if (CGSizeEqualToSize(imageSize, [recentlyResizedImage size]))
{
return recentlyResizedImage;
}
//TODO: Tweak quality depending on how large the result will be.
CGInterpolationQuality quality = kCGInterpolationDefault;
[self setRecentlyResizedImage:[UIImageResize
resizedImage:imageSize
interpolationQuality:quality
image:fullImage
hires:hires]];
return recentlyResizedImage;
}
-(UIImage *)imageForSize:(CGSize)imageSize
{
return [self imageForSize:imageSize scalingStyle:TiImageScalingDefault];
}
-(ImageCacheEntry*)initWithURL:(NSURL *)url
{
if (self = [super init]) {
remoteURL = [url retain];
local = NO;
if ([remoteURL isFileURL]) {
localPath = [[remoteURL path] retain];
local = YES;
lastModified = [[[[NSFileManager defaultManager] attributesOfItemAtPath:localPath error:nil] objectForKey:NSFileModificationDate] retain];
}
else {
localPath = [[ImageCacheEntry cachePathForURL:url] retain];
}
}
return self;
}
- (void) dealloc
{
RELEASE_TO_NIL(localPath);
RELEASE_TO_NIL(recentlyResizedImage);
RELEASE_TO_NIL(stretchableImage);
RELEASE_TO_NIL(fullImage);
RELEASE_TO_NIL(remoteURL);
RELEASE_TO_NIL(lastModified);
[super dealloc];
}
-(void)serialize:(NSData*)imageData
{
if (!local && imageData != nil) {
NSFileManager* fm = [NSFileManager defaultManager];
NSString* path = localPath;
if (hires && [TiUtils isRetinaDisplay]) { // Save as @2x w/retina
path = [NSString stringWithFormat:@"%@@2x.%@", [localPath stringByDeletingPathExtension], [localPath pathExtension]];
}
if ([fm isDeletableFileAtPath:path]) {
[fm removeItemAtPath:path error:nil];
}
if (![fm createFileAtPath:path contents:imageData attributes:nil]) {
NSLog(@"[ERROR] Unknown error serializing image %@ to path %@", remoteURL, path);
}
}
}
-(NSString*)description
{
return [NSString stringWithFormat:@"<ImageCache:%x> %@[%@]",self,remoteURL,localPath];
}
+(NSString*)cachePathForURL:(NSURL *)url
{
if ([url isFileURL]) {
return [url path];
}
NSFileManager* fm = [NSFileManager defaultManager];
NSError* error = nil;
NSURL* cacheFile = [fm URLForDirectory:NSCachesDirectory
inDomain:(NSUserDomainMask | NSLocalDomainMask)
appropriateForURL:nil
create:YES
error:&error];
if (error != nil) {
NSLog(@"[ERROR] Error finding cache directory: %@", [error localizedDescription]);
return nil;
}
NSString* urlStr = [url absoluteString];
const char* data = [urlStr UTF8String];
NSString* md5key = [TiUtils md5:[NSData dataWithBytes:data length:strlen(data)]];
cacheFile = [cacheFile URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",md5key,[url pathExtension]]];
return [cacheFile path];
}
@end
ImageLoader *sharedLoader = nil;
@implementation ImageLoaderRequest
@synthesize completed, delegate, imageSize, request;
DEFINE_EXCEPTIONS
-(void)dealloc
{
RELEASE_TO_NIL(request);
RELEASE_TO_NIL(delegate);
RELEASE_TO_NIL(userInfo);
RELEASE_TO_NIL(url);
[super dealloc];
}
-(id)initWithCallback:(NSObject<ImageLoaderDelegate>*)target_ userInfo:(id)userInfo_ url:(NSURL*)url_
{
if (self = [super init])
{
delegate = [target_ retain];
userInfo = [userInfo_ retain];
url = [url_ retain];
}
return self;
}
-(void)cancel
{
cancelled = YES;
[request cancel];
RELEASE_TO_NIL(request);
}
-(BOOL)cancelled
{
return cancelled;
}
-(id)userInfo
{
return userInfo;
}
-(NSURL*)url
{
return url;
}
@end
@implementation ImageLoader
-(id)init
{
if (self = [super init])
{
WARN_IF_BACKGROUND_THREAD_OBJ; //NSNotificationCenter is not threadsafe!
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveMemoryWarning:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
lock = [[NSRecursiveLock alloc] init];
}
return self;
}
-(void)dealloc
{
WARN_IF_BACKGROUND_THREAD_OBJ; //NSNotificationCenter is not threadsafe!
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
RELEASE_TO_NIL(cache);
RELEASE_TO_NIL(queue);
RELEASE_TO_NIL(timeout);
RELEASE_TO_NIL(lock);
[super dealloc];
}
-(void)didReceiveMemoryWarning:(id)sender
{
#ifdef DEBUG_IMAGE_CACHE
vm_statistics_data_t vmStats;
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
NSLog(@"[CACHE DEBUG] %d pages free before clearing image cache.",vmStats.free_count);
#endif
[cache removeAllObjects];
#ifdef DEBUG_IMAGE_CACHE
kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
NSLog(@"[CACHE DEBUG] %d pages free after clearing image cache.",vmStats.free_count);
#endif
}
+(ImageLoader*)sharedLoader
{
// GCD allows single-dispatch predicates, specifically for situations like singleton initialization.
// We should be switching to this pattern EVERYWHERE.
static dispatch_once_t pred;
dispatch_once(&pred, ^{
sharedLoader = [[ImageLoader alloc] init];
});
return sharedLoader;
}
-(ImageCacheEntry *)setImage:(id)image forKey:(NSURL *)url hires:(BOOL)hires;
{
NSString *urlString = [url absoluteString];
if (image==nil)
{
return nil;
}
if (cache==nil)
{
cache = [[NSCache alloc] init];
[cache setName:@"TiImageCache"];
#ifdef DEBUG_IMAGE_CACHE
[cache setDelegate:self];
NSLog(@"[CACHE DEBUG] Count limit: %d", [cache countLimit]);
NSLog(@"[CACHE DEBUG] Cost limit: %d", [cache totalCostLimit]);
#endif
}
ImageCacheEntry * newEntry = [[[ImageCacheEntry alloc] initWithURL:url] autorelease];
[newEntry setHires:hires];
if ([image isKindOfClass:[UIImage class]]) {
[newEntry setFullImage:image];
}
else if ([image isKindOfClass:[NSData class]]) {
[newEntry setData:image];
}
else {
DebugLog(@"[DEBUG] Unexpected image data type %@; not caching", [image class]);
return nil;
}
#ifdef DEBUG_IMAGE_CACHE
NSLog(@"[CACHE DEBUG] Caching: %@",newEntry);
#endif
[cache setObject:newEntry forKey:urlString];
return newEntry;
}
-(void)purge:(NSURL*)url
{
NSString* urlStr = [url absoluteString];
[cache removeObjectForKey:urlStr];
}
-(CGFloat)imageScale:(UIImage*)image
{
if ([image respondsToSelector:@selector(scale)])
{
return [image scale];
}
return 1.0;
}
-(ImageCacheEntry *)entryForKey:(NSURL *)url
{
if (url == nil)
{
return nil;
}
NSString * urlString = [url absoluteString];
ImageCacheEntry * result = [cache objectForKey:urlString];
#ifdef DEBUG_IMAGE_CACHE
NSLog(@"[CACHE DEBUG] cache[%@] : %@", urlString, result);
#endif
if (result != nil) {
if ([result local]) {
NSError* error = nil;
NSDate* currentTimeStamp = [[[NSFileManager defaultManager] attributesOfItemAtPath:result.localPath error:&error] objectForKey:NSFileModificationDate];
if (![currentTimeStamp isEqualToDate:result.lastModified]) {
//We should remove the cached image as the local file backing cached image has changed.
[self purge:url];
result = nil;
}
}
}
if (result == nil) {
if ([url isFileURL]) // Load up straight from disk
{
NSString * path = [url path];
#ifdef DEBUG_IMAGE_CACHE
NSLog(@"[CACHE DEBUG] Loading locally from path %@", path);
#endif
BOOL scaleUp = NO;
if ([TiUtils isRetinaDisplay] && [path rangeOfString:@"@2x"].location!=NSNotFound)
{
scaleUp = YES;
}
UIImage * resultImage = [UIImage imageWithContentsOfFile:path];
if (scaleUp && [self imageScale:resultImage]==1.0)
{
// on the ipad running iphone app in emulation mode, this won't exist when
// do click 2x to scale it up so we have to check for this method
if ([UIImage instancesRespondToSelector:@selector(imageWithCGImage:scale:orientation:)])
{
// if we specified a 2x, we need to upscale it
resultImage = [UIImage imageWithCGImage:[resultImage CGImage] scale:2.0 orientation:[resultImage imageOrientation]];
}
}
result = [self setImage:resultImage forKey:url hires:NO];
}
else // Check and see if we cached a file to disk
{
NSString* diskCache = [ImageCacheEntry cachePathForURL:url];
if ([[NSFileManager defaultManager] fileExistsAtPath:diskCache]) {
#ifdef DEBUG_IMAGE_CACHE
NSLog(@"[CACHE DEBUG] Retrieving local image [prefetch]: %@", diskCache);
#endif
UIImage* resultImage = [UIImage imageWithContentsOfFile:diskCache];
result = [self setImage:resultImage forKey:url hires:NO];
}
}
}
return result;
}
-(id)cache:(id)image forURL:(NSURL*)url size:(CGSize)imageSize hires:(BOOL)hires
{
return [[self setImage:image forKey:url hires:hires] imageForSize:imageSize];
}
-(id)cache:(id)image forURL:(NSURL*)url
{
return [self cache:image forURL:url size:CGSizeZero hires:NO];
}
-(id)loadRemote:(NSURL*)url
{
if (url==nil) return nil;
UIImage *image = [[self entryForKey:url] imageForSize:CGSizeZero];
if (image!=nil)
{
return image;
}
ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:url];
[req addRequestHeader:@"User-Agent" value:[[TiApp app] userAgent]];
[[TiApp app] startNetwork];
[req start];
[[TiApp app] stopNetwork];
if (req!=nil && [req error]==nil)
{
NSData *data = [req responseData];
UIImage *resultImage = [UIImage imageWithData:data];
ImageCacheEntry *result = [self setImage:resultImage forKey:url hires:NO];
[result setData:data];
return [result imageForSize:CGSizeZero];
}
return nil;
}
-(UIImage *)loadImmediateImage:(NSURL *)url
{
return [self loadImmediateImage:url withSize:CGSizeZero];
}
-(UIImage *)loadImmediateImage:(NSURL *)url withSize:(CGSize)imageSize;
{
return [[self entryForKey:url] imageForSize:imageSize];
}
-(UIImage *)loadImmediateStretchableImage:(NSURL *)url
{
return [self loadImmediateStretchableImage:url withLeftCap:TiDimensionAuto topCap:TiDimensionAuto];
}
-(UIImage *)loadImmediateStretchableImage:(NSURL *)url withLeftCap:(TiDimension)left topCap:(TiDimension)top
{
ImageCacheEntry* image = [self entryForKey:url];
image.leftCap = left;
image.topCap = top;
return [image stretchableImage];
}
-(CGSize)fullImageSize:(NSURL *)url
{
ImageCacheEntry* image = [self entryForKey:url];
if (image!=nil)
{
return [[image fullImage] size];
}
else
{
return CGSizeZero;
}
}
-(void)notifyRequest:(ImageLoaderRequest*) request imageCompleted:(UIImage*)image
{
[[request delegate] imageLoadSuccess:request image:image];
[request setRequest:nil];
}
-(void)doImageLoader:(ImageLoaderRequest*)request
{
NSURL *url = [request url];
UIImage *image = [[self entryForKey:url] imageForSize:[request imageSize]];
if (image!=nil)
{
TiThreadPerformOnMainThread(^{[self notifyRequest:request imageCompleted:image];}, NO);
return;
}
// we don't have it local or in the cache so we need to fetch it remotely
if (queue == nil)
{
queue = [[ASINetworkQueue alloc] init];
[queue setMaxConcurrentOperationCount:4];
[queue setShouldCancelAllRequestsOnFailure:NO];
[queue setDelegate:self];
[queue setRequestDidFailSelector:@selector(queueRequestDidFail:)];
[queue setRequestDidFinishSelector:@selector(queueRequestDidFinish:)];
[queue go];
}
NSDictionary *dict = [NSDictionary dictionaryWithObject:request forKey:@"request"];
ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:url];
[req setUserInfo:dict];
[req setRequestMethod:@"GET"];
[req addRequestHeader:@"User-Agent" value:[[TiApp app] userAgent]];
[req setTimeOutSeconds:20];
[request setRequest:req];
[[TiApp app] startNetwork];
[queue addOperation:req];
}
-(ImageLoaderRequest*)loadImage:(NSURL*)url delegate:(NSObject<ImageLoaderDelegate>*)delegate userInfo:(NSDictionary*)userInfo
{
ImageLoaderRequest *request = [[[ImageLoaderRequest alloc] initWithCallback:delegate userInfo:userInfo url:url] autorelease];
// if have a queue and it's suspend, just throw our request
// in the timeout queue until we're resumed
if (queue!=nil && [queue isSuspended])
{
[lock lock];
if (timeout==nil)
{
timeout = [[NSMutableArray alloc] initWithCapacity:4];
}
[timeout addObject:request];
[lock unlock];
return request;
}
[self doImageLoader:request];
return request;
}
-(void)suspend
{
[lock lock];
if (queue!=nil)
{
[queue setSuspended:YES];
}
[lock unlock];
}
-(void)cancel
{
//NOTE: this should only be called on suspend
//to cause the queue to be stopped
[lock lock];
if (queue!=nil)
{
[queue reset];
}
[lock unlock];
}
-(void)resume
{
[lock lock];
if (queue!=nil)
{
[queue setSuspended:NO];
}
if (timeout!=nil)
{
for (ImageLoaderRequest *request in timeout)
{
if ([request cancelled])
{
if ([[request delegate] respondsToSelector:@selector(imageLoadCancelled:)])
{
[[request delegate] performSelector:@selector(imageLoadCancelled:) withObject:request];
}
}
else
{
[self doImageLoader:request];
}
}
[timeout removeAllObjects];
}
[lock unlock];
}
#pragma mark Delegates
-(void)queueRequestDidFinish:(ASIHTTPRequest*)request
{
ENSURE_UI_THREAD_1_ARG(request);
// hold while we're working with it (release below)
[request retain];
[[TiApp app] stopNetwork];
ImageLoaderRequest *req = [[request userInfo] objectForKey:@"request"];
if ([req cancelled]==NO)
{
NSData *data = [request responseData];
if (data == nil || [data length]==0)
{
NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
[errorDetail setValue:@"Response returned nil" forKey:NSLocalizedDescriptionKey];
NSError *error = [NSError errorWithDomain:@"com.appcelerator.titanium.imageloader" code:1 userInfo:errorDetail];
[[req delegate] imageLoadFailed:req error:error];
[request setUserInfo:nil];
[request release];
return;
}
// we want to be able to cache remote images so we need to
// honor cache control parameters - however, we're only caching
// for this session and not on disk so we ignore (potentially at a determinent?)
// the actual max-age setting for now.
BOOL cacheable = YES;
NSString *cacheControl = [[request responseHeaders] objectForKey:@"Cache-Control"];
if (cacheControl!=nil)
{
// check to see if we're cacheable or not
NSRange range = [cacheControl rangeOfString:@"max-age=0"];
if (range.location!=NSNotFound)
{
cacheable = NO;
}
else
{
range = [cacheControl rangeOfString:@"no-cache"];
if (range.location!=NSNotFound)
{
cacheable = NO;
}
}
}
// Previously, we were creating the image here, then caching the image, then setting the data.
// This created TWO images in memory from the same binary data, which the system might not be
// smart enough to avoid dual allocations of (and for big remote images: Obviously a problem).
// So now, we cache (if we can) and then pull the created image, or just create the image if
// we need to, and then dump the entry from the cache if there was a problem.
UIImage* image = nil;
if (cacheable)
{
BOOL hires = [TiUtils boolValue:[[req userInfo] valueForKey:@"hires"] def:NO];
[self cache:data forURL:[req url] size:CGSizeZero hires:hires];
ImageCacheEntry *entry = [self entryForKey:[req url]];
image = [entry fullImage];
}
else {
image = [UIImage imageWithData:data];
}
if (image == nil)
{
if (cacheable) {
[self purge:[req url]];
}
NSMutableDictionary* errorDetail = [NSMutableDictionary dictionary];
[errorDetail setValue:@"Returned invalid image data" forKey:NSLocalizedDescriptionKey];
NSError* error = [NSError errorWithDomain:@"com.appcelerator.titanium.imageloader" code:1 userInfo:errorDetail];
[[req delegate] imageLoadFailed:req error:error];
[request setUserInfo:nil];
[request release];
return;
}
[self notifyRequest:req imageCompleted:image];
}
else
{
if ([[req delegate] respondsToSelector:@selector(imageLoadCancelled:)])
{
[[req delegate] performSelector:@selector(imageLoadCancelled:) withObject:req];
}
}
[request setUserInfo:nil];
[request release];
}
-(void)queueRequestDidFail:(ASIHTTPRequest*)request
{
[[TiApp app] stopNetwork];
ImageLoaderRequest *req = [[request userInfo] objectForKey:@"request"];
NSError *error = [request error];
if ([error code] == ASIRequestCancelledErrorType && [error domain] == NetworkRequestErrorDomain)
{
if ([[req delegate] respondsToSelector:@selector(imageLoadCancelled:)])
{
[[req delegate] performSelector:@selector(imageLoadCancelled:) withObject:req];
}
}
else
{
[[req delegate] imageLoadFailed:req error:[request error]];
}
[request setUserInfo:nil];
}
-(void)cache:(NSCache *)cache willEvictObject:(id)obj
{
#ifdef DEBUG_IMAGE_CACHE
NSLog(@"[CACHE DEBUG] Purging image cache object %@", obj);
#endif
}
@end