Add cache control for iOS.

This commit is contained in:
Dylan Vann 2018-06-13 23:47:10 -04:00
parent 30a2ff7909
commit cf9a595ee9
5 changed files with 56 additions and 11 deletions

View File

@ -7,6 +7,12 @@ typedef NS_ENUM(NSInteger, FFFPriority) {
FFFPriorityHigh
};
typedef NS_ENUM(NSInteger, FFFCacheControl) {
FFFCacheControlImmutable,
FFFCacheControlWeb,
FFFCacheControlCacheOnly
};
// Object containing an image uri and metadata.
@interface FFFastImageSource : NSObject
@ -16,9 +22,12 @@ typedef NS_ENUM(NSInteger, FFFPriority) {
@property (nonatomic) FFFPriority priority;
// headers for the image request
@property (nonatomic) NSDictionary *headers;
// cache control mode
@property (nonatomic) FFFCacheControl cacheControl;
- (instancetype)initWithURL:(NSURL *)url
priority:(FFFPriority)priority
headers:(NSDictionary *)headers;
headers:(NSDictionary *)headers
cacheControl:(FFFCacheControl)cacheControl;
@end

View File

@ -5,12 +5,14 @@
- (instancetype)initWithURL:(NSURL *)url
priority:(FFFPriority)priority
headers:(NSDictionary *)headers
cacheControl:(FFFCacheControl)cacheControl
{
self = [super init];
if (self) {
_url = url;
_priority = priority;
_headers = headers;
_cacheControl = cacheControl;
}
return self;
}

View File

@ -114,6 +114,17 @@
break;
}
switch (_source.cacheControl) {
case FFFCacheControlWeb:
options |= SDWebImageRefreshCached;
break;
case FFFCacheControlCacheOnly:
options |= SDWebImageCacheMemoryOnly;
break;
case FFFCacheControlImmutable:
break;
}
if (_onFastImageLoadStart) {
_onFastImageLoadStart(@{});
hasSentOnLoadStart = YES;

View File

@ -4,10 +4,16 @@
@implementation RCTConvert (FFFastImage)
RCT_ENUM_CONVERTER(FFFPriority, (@{
@"low": @(FFFPriorityLow),
@"normal": @(FFFPriorityNormal),
@"high": @(FFFPriorityHigh),
}), FFFPriorityNormal, integerValue);
@"low": @(FFFPriorityLow),
@"normal": @(FFFPriorityNormal),
@"high": @(FFFPriorityHigh),
}), FFFPriorityNormal, integerValue);
RCT_ENUM_CONVERTER(FFFCacheControl, (@{
@"immutable": @(FFFCacheControlImmutable),
@"web": @(FFFCacheControlWeb),
@"cacheOnly": @(FFFCacheControlCacheOnly),
}), FFFCacheControlImmutable, integerValue);
+ (FFFastImageSource *)FFFastImageSource:(id)json {
if (!json) {
@ -18,14 +24,15 @@ RCT_ENUM_CONVERTER(FFFPriority, (@{
NSURL *uri = [self NSURL:uriString];
FFFPriority priority = [self FFFPriority:json[@"priority"]];
FFFCacheControl cacheControl = [self FFFCacheControl:json[@"cache"]];
NSDictionary *headers = [self NSDictionary:json[@"headers"]];
if (headers) {
__block BOOL allHeadersAreStrings = YES;
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id header, BOOL *stop) {
if (![header isKindOfClass:[NSString class]]) {
RCTLogError(@"Values of HTTP headers passed must be of type string. "
"Value of header '%@' is not a string.", key);
"Value of header '%@' is not a string.", key);
allHeadersAreStrings = NO;
*stop = YES;
}
@ -35,9 +42,9 @@ RCT_ENUM_CONVERTER(FFFPriority, (@{
headers = nil;
}
}
FFFastImageSource *imageSource = [[FFFastImageSource alloc] initWithURL:uri priority:priority headers:headers];
FFFastImageSource *imageSource = [[FFFastImageSource alloc] initWithURL:uri priority:priority headers:headers cacheControl:cacheControl];
return imageSource;
}

View File

@ -39,7 +39,10 @@ class FastImage extends Component {
if (fallback) {
return (
<View style={[style, styles.imageContainer]} ref={this.captureRef}>
<View
style={[style, styles.imageContainer]}
ref={this.captureRef}
>
<FastImageView
{...props}
style={StyleSheet.absoluteFill}
@ -87,11 +90,23 @@ FastImage.resizeMode = {
}
FastImage.priority = {
// lower than usual.
low: 'low',
// normal, the default.
normal: 'normal',
// higher than usual.
high: 'high',
}
FastImage.cacheControl = {
// Ignore headers, use uri as cache key, fetch only if not in cache.
immutable: 'immutable',
// Respect http headers, no aggressive caching.
web: 'web',
// Only load from cache.
cacheOnly: 'cacheOnly',
}
FastImage.preload = sources => {
FastImageViewNativeModule.preload(sources)
}
@ -104,6 +119,7 @@ const FastImageSourcePropType = PropTypes.shape({
uri: PropTypes.string,
headers: PropTypes.objectOf(PropTypes.string),
priority: PropTypes.oneOf(Object.keys(FastImage.priority)),
cache: PropTypes.oneOf(Object.keys(FastImage.cacheControl)),
})
FastImage.propTypes = {