115 lines
3.8 KiB
Mathematica
Raw Normal View History

2017-04-13 00:13:44 -04:00
#import "FFFastImageView.h"
@implementation FFFastImageView {
BOOL hasSentOnLoadStart;
BOOL isComplete;
BOOL hasError;
}
2017-04-13 00:13:44 -04:00
- (void)setResizeMode:(RCTResizeMode)resizeMode
{
if (_resizeMode != resizeMode) {
_resizeMode = resizeMode;
self.contentMode = (UIViewContentMode)resizeMode;
}
}
- (void)setOnFastImageLoadEnd:(RCTBubblingEventBlock)onFastImageLoadEnd {
_onFastImageLoadEnd = onFastImageLoadEnd;
if (isComplete) {
_onFastImageLoadEnd(@{});
}
}
- (void)setOnFastImageLoad:(RCTBubblingEventBlock)onFastImageLoad {
_onFastImageLoad = onFastImageLoad;
if (isComplete && hasError == NO) {
_onFastImageLoad(@{});
}
}
- (void)setOnFastImageLoadStart:(RCTBubblingEventBlock)onFastImageLoadStart {
if (_source && !hasSentOnLoadStart) {
_onFastImageLoadStart = onFastImageLoadStart;
onFastImageLoadStart(@{});
hasSentOnLoadStart = YES;
} else {
_onFastImageLoadStart = onFastImageLoadStart;
hasSentOnLoadStart = NO;
}
}
2017-04-13 00:13:44 -04:00
- (void)setSource:(FFFastImageSource *)source {
if (_source != source) {
isComplete = NO;
hasError = NO;
_source = source;
2017-04-13 00:13:44 -04:00
// Set headers.
[_source.headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString* header, BOOL *stop) {
2017-04-13 00:13:44 -04:00
[[SDWebImageDownloader sharedDownloader] setValue:header forHTTPHeaderField:key];
}];
// Set priority.
SDWebImageOptions options = 0;
options |= SDWebImageRetryFailed;
switch (_source.priority) {
2017-04-13 00:13:44 -04:00
case FFFPriorityLow:
options |= SDWebImageLowPriority;
break;
case FFFPriorityNormal:
// Priority is normal by default.
break;
case FFFPriorityHigh:
options |= SDWebImageHighPriority;
break;
}
if (_onFastImageLoadStart) {
_onFastImageLoadStart(@{});
hasSentOnLoadStart = YES;
} {
hasSentOnLoadStart = NO;
}
2017-04-13 00:13:44 -04:00
// Load the new source.
[self sd_setImageWithURL:_source.uri
2017-04-13 00:13:44 -04:00
placeholderImage:nil
options:options
2017-07-03 21:58:24 -04:00
progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
double progress = MIN(1, MAX(0, (double) receivedSize / (double) expectedSize));
if (_onFastImageProgress) {
_onFastImageProgress(@{
@"loaded": @(receivedSize),
@"total": @(expectedSize)
});
2017-07-03 21:58:24 -04:00
}
} completed:^(UIImage * _Nullable image,
NSError * _Nullable error,
SDImageCacheType cacheType,
NSURL * _Nullable imageURL) {
isComplete = YES;
2017-07-03 21:58:24 -04:00
if (error) {
hasError = YES;
2017-07-03 21:58:24 -04:00
if (_onFastImageError) {
_onFastImageError(@{});
if (_onFastImageLoadEnd) {
_onFastImageLoadEnd(@{});
}
2017-07-03 21:58:24 -04:00
}
} else {
if (_onFastImageLoad) {
_onFastImageLoad(@{});
if (_onFastImageLoadEnd) {
_onFastImageLoadEnd(@{});
}
2017-07-03 21:58:24 -04:00
}
}
}];
2017-04-13 00:13:44 -04:00
}
}
@end
2017-07-03 21:58:24 -04:00