-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBee_UIImageView.m
More file actions
974 lines (785 loc) · 20.2 KB
/
Bee_UIImageView.m
File metadata and controls
974 lines (785 loc) · 20.2 KB
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
//
// ______ ______ ______
// /\ __ \ /\ ___\ /\ ___\
// \ \ __< \ \ __\_ \ \ __\_
// \ \_____\ \ \_____\ \ \_____\
// \/_____/ \/_____/ \/_____/
//
//
// Copyright (c) 2013-2014, {Bee} open source community
// http://www.bee-framework.com
//
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
#import "Bee_UIImageView.h"
#import "Bee_UIConfig.h"
#import "Bee_UISignalBus.h"
#import "Bee_UIMetrics.h"
#import "Bee_Cache.h"
#import "Bee_Network.h"
#import "Bee_UIActivityIndicatorView.h"
#import "UIImage+BeeExtension.h"
#import "UIView+BeeUISignal.h"
#import "UIView+LifeCycle.h"
#import "UIView+Transition.h"
#pragma mark -
DEF_PACKAGE( BeePackage_UI, BeeImageCache, imageCache );
#pragma mark -
@interface BeeImageCache()
{
BeeMemoryCache * _memoryCache;
BeeFileCache * _fileCache;
}
@property (atomic, retain) BeeMemoryCache * memoryCache;
@property (atomic, retain) BeeFileCache * fileCache;
@end
#pragma mark -
@implementation BeeImageCache
DEF_SINGLETON( BeeImageCache )
@synthesize memoryCache = _memoryCache;
@synthesize fileCache = _fileCache;
- (id)init
{
self = [super init];
if ( self )
{
_memoryCache = [[BeeMemoryCache alloc] init];
_memoryCache.clearWhenMemoryLow = YES;
_fileCache = [[BeeFileCache alloc] init];
_fileCache.cachePath = [NSString stringWithFormat:@"%@/ImageCache/", [BeeSandbox libCachePath]];
_fileCache.cacheUser = @"";
}
return self;
}
- (void)dealloc
{
self.memoryCache = nil;
self.fileCache = nil;
[super dealloc];
}
- (BOOL)hasCachedForURL:(NSString *)string
{
if ( nil == string || 0 == string.length )
return NO;
NSString * cacheKey = [string MD5];
BOOL flag = [self.memoryCache hasObjectForKey:cacheKey];
if ( NO == flag )
{
flag = [self.fileCache hasObjectForKey:cacheKey];
}
return flag;
}
- (BOOL)hasFileCachedForURL:(NSString *)url
{
if ( nil == url || 0 == url.length )
return NO;
NSString * cacheKey = [url MD5];
return [self.fileCache hasObjectForKey:cacheKey];
}
- (BOOL)hasMemoryCachedForURL:(NSString *)url
{
if ( nil == url || 0 == url.length )
return NO;
NSString * cacheKey = [url MD5];
return [self.memoryCache hasObjectForKey:cacheKey];
}
- (UIImage *)fileImageForURL:(NSString *)url
{
if ( nil == url || 0 == url.length )
return nil;
PERF_ENTER
NSString * cacheKey = [url MD5];
UIImage * image = nil;
NSString * fullPath = [self.fileCache fileNameForKey:cacheKey];
if ( fullPath )
{
image = [[[UIImage alloc] initWithContentsOfFile:fullPath] autorelease];
UIImage * cachedImage = (UIImage *)[self.memoryCache objectForKey:cacheKey];
if ( nil == cachedImage && image != cachedImage )
{
[self.memoryCache setObject:image forKey:cacheKey];
}
}
PERF_LEAVE
return image;
}
- (UIImage *)memoryImageForURL:(NSString *)url
{
if ( nil == url || 0 == url.length )
return nil;
PERF_ENTER
NSString * cacheKey = [url MD5];
UIImage * image = nil;
NSObject * object = [self.memoryCache objectForKey:cacheKey];
if ( object && [object isKindOfClass:[UIImage class]] )
{
image = (UIImage *)object;
}
PERF_LEAVE
return image;
}
- (UIImage *)imageForURL:(NSString *)string
{
if ( nil == string || 0 == string.length )
return nil;
UIImage * image = [self memoryImageForURL:string];
if ( nil == image )
{
image = [self fileImageForURL:string];
}
return image;
}
- (void)saveImage:(UIImage *)image forURL:(NSString *)string
{
PERF_ENTER
NSString * cacheKey = [string MD5];
UIImage * cachedImage = (UIImage *)[self.memoryCache objectForKey:cacheKey];
if ( nil == cachedImage && image != cachedImage )
{
[self.memoryCache setObject:image forKey:cacheKey];
}
PERF_LEAVE
}
- (void)saveData:(NSData *)data forURL:(NSString *)string
{
PERF_ENTER
NSString * cacheKey = [string MD5];
[self.fileCache setObject:data forKey:cacheKey];
PERF_LEAVE
}
- (void)deleteImageForURL:(NSString *)string
{
PERF_ENTER
NSString * cacheKey = [string MD5];
[self.memoryCache removeObjectForKey:cacheKey];
[self.fileCache removeObjectForKey:cacheKey];
PERF_LEAVE
}
- (void)deleteAllImages
{
[self.memoryCache removeAllObjects];
[self.fileCache removeAllObjects];
}
@end
#pragma mark -
@interface BeeUIImageView()
{
BOOL _inited;
BOOL _gray;
BOOL _round;
BOOL _pattern;
BOOL _strech;
UIEdgeInsets _strechInsets;
BOOL _loading;
BeeUILabel * _altLabel;
BeeUIActivityIndicatorView * _indicator;
NSString * _loadedURL;
BOOL _loaded;
UIImage * _defaultImage;
}
- (void)initSelf;
- (void)changeImage:(UIImage *)image;
- (void)changeImage:(UIImage *)image animated:(BOOL)animated;
@end
@implementation BeeUIImageView
DEF_SIGNAL( LOAD_START )
DEF_SIGNAL( LOAD_COMPLETED )
DEF_SIGNAL( LOAD_FAILED )
DEF_SIGNAL( LOAD_CANCELLED )
DEF_SIGNAL( LOAD_CACHE )
//DEF_SIGNAL( WILL_CHANGE )
//DEF_SIGNAL( DID_CHANGED )
@synthesize gray = _gray;
@synthesize round = _round;
@synthesize pattern = _pattern;
@synthesize strech = _strech;
@synthesize strechInsets = _strechInsets;
@synthesize loading = _loading;
@synthesize altLabel = _altLabel;
@synthesize indicator = _indicator;
@dynamic indicatorStyle;
@dynamic indicatorColor;
@synthesize loadedURL = _loadedURL;
@synthesize loaded = _loaded;
@synthesize defaultImage = _defaultImage;
@synthesize enableAllEvents = _enableAllEvents;
@synthesize url;
@synthesize file;
@synthesize resource;
- (id)init
{
self = [super init];
if ( self )
{
[self initSelf];
}
return self;
}
- (id)initWithImage:(UIImage *)image
{
self = [super initWithImage:image];
if ( self )
{
[self initSelf];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if ( self )
{
[self initSelf];
}
return self;
}
- (void)initSelf
{
if ( NO == _inited )
{
self.hidden = NO;
self.backgroundColor = [UIColor clearColor];
self.contentMode = UIViewContentModeCenter;
self.layer.masksToBounds = YES;
self.layer.opaque = YES;
_loading = NO;
_loaded = NO;
_gray = NO;
_round = NO;
_pattern = NO;
_strech = NO;
_strechInsets = UIEdgeInsetsZero;
_inited = YES;
// [self load];
[self performLoad];
}
}
- (void)dealloc
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
// [self unload];
[self performUnload];
[self cancelRequests];
self.loadedURL = nil;
self.loadedCroppedURL = nil;
self.loading = NO;
self.defaultImage = nil;
self.image = nil;
[_indicator removeFromSuperview];
[_indicator release];
[_altLabel removeFromSuperview];
[_altLabel release];
[super dealloc];
}
- (void)GET:(NSString *)string useCache:(BOOL)useCache
{
[self GET:string useCache:useCache placeHolder:nil];
}
- (void)GET:(NSString *)newURL useCache:(BOOL)useCache placeHolder:(UIImage *)defaultImage
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
if ( nil == newURL || 0 == newURL.length )
{
[self changeImage:self.defaultImage];
return;
}
// if ( NO == [newURL hasPrefix:@"http://"] )
// {
// newURL = [NSString stringWithFormat:@"http://%@", newURL];
// }
if ( [self requestingURL:newURL] )
{
// [self setNeedsDisplay];
return;
}
// if ( self.loadedURL && [self.loadedURL isEqualToString:newURL] )
// {
//// [self setNeedsDisplay];
// return;
// }
self.defaultImage = defaultImage;
self.loadedURL = newURL;
if ( self.crop )
{
self.loadedCroppedURL = [newURL stringByAppendingFormat:@"-w%.f-h%.f",
self.cropSize.width,
self.cropSize.height];
}
self.loading = NO;
self.loaded = NO;
[self cancelRequests];
BeeImageCache * cache = [BeeImageCache sharedInstance];
if ( self.crop )
{
if ( [cache hasCachedForURL:self.loadedCroppedURL] )
{
// [self changeImage:nil];
// [self performSelector:@selector(loadFromCache:) withObject:newURL afterDelay:0.25f];
[self loadFromCache:self.loadedCroppedURL];
}
else
{
[self changeImage:self.defaultImage];
[self performSelector:@selector(loadFromWeb:) withObject:newURL afterDelay:0.25f];
}
}
else
{
if ( [cache hasCachedForURL:newURL] )
{
// [self changeImage:nil];
// [self performSelector:@selector(loadFromCache:) withObject:newURL afterDelay:0.25f];
[self loadFromCache:newURL];
}
else
{
[self changeImage:self.defaultImage];
[self performSelector:@selector(loadFromWeb:) withObject:newURL afterDelay:0.25f];
}
}
}
- (void)loadFromCache:(NSString *)newURL
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
BeeImageCache * cache = [BeeImageCache sharedInstance];
if ( [cache hasCachedForURL:newURL] )
{
UIImage * image = [cache memoryImageForURL:newURL];
if ( image )
{
[self changeImage:image];
[self setLoaded:YES];
if ( self.enableAllEvents )
{
[self sendUISignal:BeeUIImageView.LOAD_CACHE];
}
}
else
{
[self changeImage:self.defaultImage];
[self performSelector:@selector(loadFromFile:) withObject:newURL afterDelay:0.1f];
}
}
else
{
[self changeImage:self.defaultImage];
[self performSelector:@selector(loadFromWeb:) withObject:newURL afterDelay:0.1f];
}
}
- (void)loadFromFile:(NSString *)newURL
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
BeeImageCache * cache = [BeeImageCache sharedInstance];
if ( [cache hasCachedForURL:newURL] )
{
NSString * camparedURL = self.crop ? self.loadedCroppedURL : self.loadedURL;
if ( [newURL isEqualToString:camparedURL] )
{
UIImage * image = [cache imageForURL:newURL];
if ( image )
{
if ( [newURL isEqualToString:camparedURL] )
{
[self changeImage:image animated:YES];
[self setLoaded:YES];
if ( self.enableAllEvents )
{
[self sendUISignal:BeeUIImageView.LOAD_CACHE];
}
}
return;
}
}
return;
}
[self changeImage:self.defaultImage];
[self performSelector:@selector(loadFromWeb:) withObject:newURL afterDelay:0.1f];
}
- (void)loadFromWeb:(NSString *)newURL
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self HTTP_GET:newURL].timeOutSeconds = 45.0f;
}
- (void)setUrl:(NSString *)string
{
[self GET:string useCache:YES];
}
- (void)setFile:(NSString *)path
{
if ( nil == path )
return;
UIImage * image = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
if ( image )
{
[self changeImage:image];
}
else
{
[self changeImage:self.defaultImage];
}
}
- (void)setResource:(NSString *)string
{
UIImage * image = [UIImage imageNamed:string];
if ( image )
{
[self changeImage:image];
}
else
{
[self changeImage:self.defaultImage];
}
}
- (void)setImage:(UIImage *)image
{
[self changeImage:image];
}
- (void)changeImage:(UIImage *)image
{
[self changeImage:image animated:NO];
}
- (void)changeImage:(UIImage *)image animated:(BOOL)animated
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
if ( nil == image )
{
[self cancelRequests];
// self.loadedURL = nil;
// self.loading = NO;
// self.loaded = NO;
[super setImage:nil];
// [super setNeedsDisplay];
return;
}
if ( image != self.image )
{
PERF_ENTER
[self cancelRequests];
UIColor * backgroundColor = nil;
if ( self.round )
{
image = [image rounded];
}
if ( self.gray )
{
image = [image grayscale];
}
if ( self.strech )
{
if ( NO == UIEdgeInsetsEqualToEdgeInsets(_strechInsets, UIEdgeInsetsZero) )
{
image = [image stretched:_strechInsets];
}
else
{
image = [image stretched];
}
}
if ( self.pattern )
{
backgroundColor = [UIColor colorWithPatternImage:image];
}
if ( backgroundColor )
{
// [self sendUISignal:BeeUIImageView.WILL_CHANGE];
[super setBackgroundColor:backgroundColor];
[super setImage:nil];
// [self sendUISignal:BeeUIImageView.DID_CHANGED];
}
else
{
CGAffineTransform transform = CGAffineTransformIdentity;
UIImageOrientation orientation = image.imageOrientation;
switch ( orientation )
{
case UIImageOrientationDown: // EXIF = 3
case UIImageOrientationDownMirrored: // EXIF = 4
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft: // EXIF = 6
case UIImageOrientationLeftMirrored: // EXIF = 5
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight: // EXIF = 8
case UIImageOrientationRightMirrored: // EXIF = 7
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
}
// [self sendUISignal:BeeUIImageView.WILL_CHANGE];
[super setTransform:transform];
if ( animated )
{
[self transitionFade];
}
[super setImage:image];
// [self sendUISignal:BeeUIImageView.DID_CHANGED];
}
PERF_LEAVE
}
// [self setNeedsDisplay];
}
#pragma mark -
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
if ( _indicator )
{
CGRect indicatorFrame;
indicatorFrame.size.width = 20.0f;
indicatorFrame.size.height = 20.0f;
indicatorFrame.origin.x = (frame.size.width - indicatorFrame.size.width) / 2.0f;
indicatorFrame.origin.y = (frame.size.height - indicatorFrame.size.height) / 2.0f;
_indicator.frame = indicatorFrame;
}
if ( _altLabel )
{
_altLabel.frame = CGRectMake( 0, 0, frame.size.width, frame.size.height );
}
}
- (void)setHidden:(BOOL)flag
{
[super setHidden:flag];
}
#pragma mark -
- (void)clear
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self cancelRequests];
[self changeImage:nil];
// self.defaultImage = nil;
self.loadedURL = nil;
self.loading = NO;
}
- (BeeUILabel *)altLabel
{
if ( nil == _altLabel )
{
_altLabel = [[BeeUILabel alloc] initWithFrame:self.bounds];
_altLabel.backgroundColor = [UIColor clearColor];
_altLabel.lineBreakMode = UILineBreakModeTailTruncation;
_altLabel.numberOfLines = 1;
_altLabel.textColor = [UIColor blackColor];
_altLabel.textAlignment = UITextAlignmentCenter;
if ( IOS7_OR_LATER )
{
_altLabel.font = [UIFont systemFontOfSize:12.0f];
}
else
{
_altLabel.font = [UIFont boldSystemFontOfSize:12.0f];
}
[self addSubview:_altLabel];
[self bringSubviewToFront:_altLabel];
}
return _altLabel;
}
- (BeeUIActivityIndicatorView *)indicator
{
if ( nil == _indicator )
{
CGRect indicatorFrame;
indicatorFrame.size.width = 20.0f;
indicatorFrame.size.height = 20.0f;
indicatorFrame.origin.x = (self.frame.size.width - indicatorFrame.size.width) / 2.0f;
indicatorFrame.origin.y = (self.frame.size.height - indicatorFrame.size.height) / 2.0f;
_indicator = [[BeeUIActivityIndicatorView alloc] initWithFrame:indicatorFrame];
_indicator.backgroundColor = [UIColor clearColor];
[self addSubview:_indicator];
}
return _indicator;
}
- (UIActivityIndicatorViewStyle)indicatorStyle
{
return self.indicator.activityIndicatorViewStyle;
}
- (void)setIndicatorStyle:(UIActivityIndicatorViewStyle)value
{
self.indicator.activityIndicatorViewStyle = value;
}
- (UIColor *)indicatorColor
{
if ( [self.indicator respondsToSelector:@selector(color)] )
{
return self.indicator.color;
}
return [UIColor clearColor];
}
- (void)setIndicatorColor:(UIColor *)color
{
if ( [self.indicator respondsToSelector:@selector(setColor:)] )
{
self.indicator.color = color;
}
}
#pragma mark -
#pragma mark NetworkRequestDelegate
- (void)handleRequest:(BeeHTTPRequest *)request
{
if ( request.sending )
{
[_indicator startAnimating];
[self setLoading:YES];
if ( self.enableAllEvents )
{
[self sendUISignal:BeeUIImageView.LOAD_START];
}
}
else if ( request.sendProgressed )
{
}
else if ( request.recving )
{
}
else if ( request.recvProgressed )
{
}
else if ( request.succeed )
{
PERF_ENTER
[_indicator stopAnimating];
NSData * data = [request responseData];
if ( data )
{
UIImage * image = [UIImage imageWithData:data];
if ( image )
{
NSString * string = [request.url absoluteString];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BeeImageCache * cache = [BeeImageCache sharedInstance];
UIImage * cacheImage = nil;
if ( self.crop )
{
cacheImage = [image crop2:CGRectMakeBound(self.cropSize.width, self.cropSize.height)];
[cache saveImage:cacheImage forURL:self.loadedCroppedURL];
NSData * croppedData = UIImagePNGRepresentation(cacheImage);
if ( nil == croppedData )
{
croppedData = UIImageJPEGRepresentation(cacheImage, 0.6);
}
if ( croppedData )
{
[cache saveData:croppedData forURL:self.loadedCroppedURL];
}
}
if ( nil == cacheImage )
{
cacheImage = image;
}
[cache saveImage:image forURL:string];
[cache saveData:data forURL:string];
dispatch_sync(dispatch_get_main_queue(), ^{
[self setLoading:NO];
[self setLoaded:YES];
if ( self.enableAllEvents )
{
[self sendUISignal:BeeUIImageView.LOAD_COMPLETED];
}
[self changeImage:cacheImage animated:YES];
});
});
}
else
{
[self setLoading:NO];
self.loaded = NO;
if ( self.enableAllEvents )
{
[self sendUISignal:BeeUIImageView.LOAD_FAILED];
}
}
}
else
{
[self setLoading:NO];
self.loaded = NO;
if ( self.enableAllEvents )
{
[self sendUISignal:BeeUIImageView.LOAD_FAILED];
}
}
PERF_LEAVE
}
else if ( request.failed )
{
[_indicator stopAnimating];
[self setLoading:NO];
[self setLoaded:NO];
if ( self.enableAllEvents )
{
[self sendUISignal:BeeUIImageView.LOAD_FAILED];
}
}
else if ( request.cancelled )
{
[_indicator stopAnimating];
[self setLoading:NO];
if ( self.enableAllEvents )
{
[self sendUISignal:BeeUIImageView.LOAD_CANCELLED];
}
}
}
#pragma mark -
#pragma mark NetworkRequestDelegate
- (void)handleUISignal:(BeeUISignal *)signal
{
SIGNAL_FORWARD( signal );
if ( [signal is:BeeUIImageView.LOAD_START] )
{
if ( _altLabel )
{
_altLabel.hidden = NO;
}
}
else if ( [signal is:BeeUIImageView.LOAD_COMPLETED] )
{
if ( _altLabel )
{
_altLabel.hidden = YES;
}
}
else if ( [signal is:BeeUIImageView.LOAD_FAILED] )
{
if ( _altLabel )
{
_altLabel.hidden = NO;
}
}
else if ( [signal is:BeeUIImageView.LOAD_CANCELLED] )
{
if ( _altLabel )
{
_altLabel.hidden = NO;
}
}
else if ( [signal is:BeeUIImageView.LOAD_CACHE] )
{
if ( _altLabel )
{
_altLabel.hidden = YES;
}
}
}
@end
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)