-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathUIImageView+SGImageCache.m
126 lines (104 loc) · 4.08 KB
/
UIImageView+SGImageCache.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
//
// Created by matt on 22/05/14.
//
#if !TARGET_OS_WATCH
#import "UIImageView+SGImageCache.h"
#import "SGImageCache.h"
#import <MGEvents/MGEvents.h>
#import <objc/runtime.h>
@interface UIImageView (SGImageCache_Private)
@property (nonatomic,strong) NSString *cachedImageURL;
@end
@implementation UIImageView (SGImageCache_Private)
@dynamic cachedImageURL;
- (void)setCachedImageURL:(NSString*)object {
objc_setAssociatedObject(self, @selector(cachedImageURL), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString*)cachedImageURL {
return objc_getAssociatedObject(self, @selector(cachedImageURL));
}
@end
@implementation UIImageView (SGImageCache)
#pragma mark - Setting Images via URL
- (void)setImageForURL:(NSString *)url {
[self setImageForURL:url placeholder:nil];
}
- (void)setImageForURL:(NSString *)url placeholder:(UIImage *)placeholder {
[self setImageForURL:url placeholder:placeholder crossFadeDuration:0];
}
- (void)setImageForURL:(NSString*)url
placeholder:(UIImage*)placeholder
stillValid:(BOOL(^)(void))stillValid {
[self setImageForURL:url placeholder:placeholder crossFadeDuration:0 stillValid:stillValid];
}
- (void)setImageForURL:(NSString *)url
placeholder:(UIImage *)placeholder
crossFadeDuration:(NSTimeInterval)duration {
[self setImageForURL:url placeholder:placeholder crossFadeDuration:duration stillValid:nil];
}
- (void)setImageForURL:(NSString *)url
placeholder:(UIImage *)placeholder
crossFadeDuration:(NSTimeInterval)duration
stillValid:(BOOL(^)(void))stillValid {
__weakSelf me = self;
self.cachedImageURL = url;
if ([SGImageCache haveImageForURL:url]) {
UIImage *image = [SGImageCache imageForURL:url];
self.image = image;
[self trigger:SGImageViewImageChanged withContext:image];
} else {
if (self.image != placeholder) {
self.image = placeholder;
[me trigger:SGImageViewImageChanged withContext:placeholder];
}
[SGImageCache getImageForURL:url].then(^(UIImage *image) {
if (!image) {
return;
}
if (url != me.cachedImageURL) {
return;
}
if (stillValid && !stillValid()) {
return;
}
if (duration > 0 && me.window) {
[UIView transitionWithView:me.superview
duration:duration
options:UIViewAnimationOptionTransitionCrossDissolve |
UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionAllowUserInteraction
animations:^{
me.image = image;
[me trigger:SGImageViewImageChanged withContext:image];
} completion:nil];
} else {
me.image = image;
[me trigger:SGImageViewImageChanged withContext:image];
}
});
}
}
#pragma mark - Setting Images via Image name
- (void)setImageWithName:(NSString *)name {
[self setImageWithName:name crossFadeDuration:0];
}
- (void)setImageWithName:(NSString *)name
crossFadeDuration:(NSTimeInterval)duration {
__weakSelf me = self;
if (duration > 0 && me.window) {
UIImage *image = [SGImageCache imageNamed:name];
[UIView transitionWithView:me.superview
duration:duration
options:UIViewAnimationOptionTransitionCrossDissolve |
UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionAllowUserInteraction
animations:^{
me.image = image;
[me trigger:SGImageViewImageChanged withContext:image];
} completion:nil];
} else {
UIImage *image = [SGImageCache imageNamed:name];
self.image = image;
[self trigger:SGImageViewImageChanged withContext:image];
}
}
@end
#endif