-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathFFFastImageView.m
More file actions
259 lines (229 loc) · 8.4 KB
/
FFFastImageView.m
File metadata and controls
259 lines (229 loc) · 8.4 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
#import "FFFastImageView.h"
#import <SDWebImage/UIImage+MultiFormat.h>
#import <SDWebImage/UIView+WebCache.h>
@interface FFFastImageView ()
@property(nonatomic, assign) BOOL hasSentOnLoadStart;
@property(nonatomic, assign) BOOL hasCompleted;
@property(nonatomic, assign) BOOL hasErrored;
// Whether the latest change of props requires the image to be reloaded
@property(nonatomic, assign) BOOL needsReload;
@property(nonatomic, strong) NSDictionary* onLoadEvent;
@end
@implementation FFFastImageView
- (id) init {
self = [super init];
self.resizeMode = RCTResizeModeCover;
self.clipsToBounds = YES;
return self;
}
- (void) setResizeMode: (RCTResizeMode)resizeMode {
if (_resizeMode != resizeMode) {
_resizeMode = resizeMode;
self.contentMode = (UIViewContentMode) resizeMode;
}
}
- (void) setOnFastImageLoadEnd: (RCTDirectEventBlock)onFastImageLoadEnd {
_onFastImageLoadEnd = onFastImageLoadEnd;
if (self.hasCompleted) {
_onFastImageLoadEnd(@{});
}
}
- (void) setOnFastImageLoad: (RCTDirectEventBlock)onFastImageLoad {
_onFastImageLoad = onFastImageLoad;
if (self.hasCompleted) {
_onFastImageLoad(self.onLoadEvent);
}
}
- (void) setOnFastImageError: (RCTDirectEventBlock)onFastImageError {
_onFastImageError = onFastImageError;
if (self.hasErrored) {
_onFastImageError(@{});
}
}
- (void) setOnFastImageLoadStart: (RCTDirectEventBlock)onFastImageLoadStart {
if (_source && !self.hasSentOnLoadStart) {
_onFastImageLoadStart = onFastImageLoadStart;
onFastImageLoadStart(@{});
self.hasSentOnLoadStart = YES;
} else {
_onFastImageLoadStart = onFastImageLoadStart;
self.hasSentOnLoadStart = NO;
}
}
- (void) setImageColor: (UIColor*)imageColor {
if (imageColor != nil) {
_imageColor = imageColor;
if (super.image) {
super.image = [self makeImage: super.image withTint: self.imageColor];
}
}
}
- (UIImage*) makeImage: (UIImage*)image withTint: (UIColor*)color {
UIImage* newImage = [image imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate];
UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale);
[color set];
[newImage drawInRect: CGRectMake(0, 0, image.size.width, newImage.size.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (void) setImage: (UIImage*)image {
if (self.imageColor != nil) {
super.image = [self makeImage: image withTint: self.imageColor];
} else {
super.image = image;
}
}
- (void) sendOnLoad: (UIImage*)image {
self.onLoadEvent = @{
@"width": [NSNumber numberWithDouble: image.size.width],
@"height": [NSNumber numberWithDouble: image.size.height]
};
if (self.onFastImageLoad) {
self.onFastImageLoad(self.onLoadEvent);
}
}
- (void) setSource: (FFFastImageSource*)source {
if (_source != source) {
_source = source;
_needsReload = YES;
}
}
- (void) setDefaultSource: (UIImage*)defaultSource {
if (_defaultSource != defaultSource) {
_defaultSource = defaultSource;
_needsReload = YES;
}
}
- (void) didSetProps: (NSArray<NSString*>*)changedProps {
if (_needsReload) {
[self reloadImage];
}
}
- (void) reloadImage {
_needsReload = NO;
if (_source) {
// Load base64 images.
NSString* url = [_source.url absoluteString];
if (url && [url hasPrefix: @"data:image"]) {
if (self.onFastImageLoadStart) {
self.onFastImageLoadStart(@{});
self.hasSentOnLoadStart = YES;
} else {
self.hasSentOnLoadStart = NO;
}
// Use SDWebImage API to support external format like WebP images
UIImage* image = [UIImage sd_imageWithData: [NSData dataWithContentsOfURL: _source.url]];
[self setImage: image];
if (self.onFastImageProgress) {
self.onFastImageProgress(@{
@"loaded": @(1),
@"total": @(1)
});
}
self.hasCompleted = YES;
[self sendOnLoad: image];
if (self.onFastImageLoadEnd) {
self.onFastImageLoadEnd(@{});
}
return;
}
// Set headers.
NSDictionary* headers = _source.headers;
SDWebImageDownloaderRequestModifier* requestModifier = [SDWebImageDownloaderRequestModifier requestModifierWithBlock: ^NSURLRequest* _Nullable (NSURLRequest* _Nonnull request) {
NSMutableURLRequest* mutableRequest = [request mutableCopy];
for (NSString* header in headers) {
NSString* value = headers[header];
[mutableRequest setValue: value forHTTPHeaderField: header];
}
return [mutableRequest copy];
}];
SDWebImageContext* context = @{SDWebImageContextDownloadRequestModifier: requestModifier};
// Set priority.
SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageHandleCookies;
switch (_source.priority) {
case FFFPriorityLow:
options |= SDWebImageLowPriority;
break;
case FFFPriorityNormal:
// Priority is normal by default.
break;
case FFFPriorityHigh:
options |= SDWebImageHighPriority;
break;
}
switch (_source.cacheControl) {
case FFFCacheControlWeb:
options |= SDWebImageRefreshCached;
break;
case FFFCacheControlCacheOnly:
options |= SDWebImageFromCacheOnly;
break;
case FFFCacheControlImmutable:
break;
}
if (self.onFastImageLoadStart) {
self.onFastImageLoadStart(@{});
self.hasSentOnLoadStart = YES;
} else {
self.hasSentOnLoadStart = NO;
}
self.hasCompleted = NO;
self.hasErrored = NO;
[self downloadImage: _source options: options context: context];
} else if (_defaultSource) {
[self setImage: _defaultSource];
}
}
- (void) downloadImage: (FFFastImageSource*)source options: (SDWebImageOptions)options context: (SDWebImageContext*)context {
UIImage *placeholderImage;
if(_source.cacheControl == FFFCacheControlCacheOnly){
if (isCached) {
placeholderImage = nil;
}
else{
placeholderImage = [UIImage imageNamed:@"defaultArticleImage"]; (*Add a placeholder image named "defaultArticleImage.png" in 1x and 2x size in ImageAssests from xcode where splash and icons are added *)
}
}
else{
placeholderImage = nil;
}
__weak typeof(self) weakSelf = self; // Always use a weak reference to self in blocks
[self sd_setImageWithURL:_source.url
placeholderImage: placeholderImage
(* [self sd_setImageWithURL: _source.url
placeholderImage: _defaultSource *)
options: options
context: context
progress: ^(NSInteger receivedSize, NSInteger expectedSize, NSURL* _Nullable targetURL) {
if (weakSelf.onFastImageProgress) {
weakSelf.onFastImageProgress(@{
@"loaded": @(receivedSize),
@"total": @(expectedSize)
});
}
} completed: ^(UIImage* _Nullable image,
NSError* _Nullable error,
SDImageCacheType cacheType,
NSURL* _Nullable imageURL) {
if (error) {
weakSelf.hasErrored = YES;
if (weakSelf.onFastImageError) {
weakSelf.onFastImageError(@{});
}
if (weakSelf.onFastImageLoadEnd) {
weakSelf.onFastImageLoadEnd(@{});
}
} else {
weakSelf.hasCompleted = YES;
[weakSelf sendOnLoad: image];
if (weakSelf.onFastImageLoadEnd) {
weakSelf.onFastImageLoadEnd(@{});
}
}
}];
}
- (void) dealloc {
[self sd_cancelCurrentImageLoad];
}
@end