Add progress events for iOS.

This commit is contained in:
Dylan Vann 2017-07-03 21:58:24 -04:00
parent 02fd00c4a6
commit ad4d8a5fe6
5 changed files with 64 additions and 28 deletions

View File

@ -1,4 +1,6 @@
import React, { PropTypes, Component } from 'react' import React, {
Component,
} from 'react'
import { import {
requireNativeComponent, requireNativeComponent,
Image, Image,
@ -16,7 +18,13 @@ class FastImage extends Component {
} }
render() { render() {
const { source, onError, onLoad, ...props } = this.props const {
source,
onError,
onLoad,
onProgress,
...props
} = this.props
// If there's no source or source uri just fallback to Image. // If there's no source or source uri just fallback to Image.
if (!source || !source.uri) { if (!source || !source.uri) {
@ -25,6 +33,7 @@ class FastImage extends Component {
ref={e => (this._root = e)} ref={e => (this._root = e)}
{...props} {...props}
source={source} source={source}
onProgress={onProgress}
onError={onError} onError={onError}
onLoad={onLoad} onLoad={onLoad}
/> />
@ -37,6 +46,7 @@ class FastImage extends Component {
ref={e => (this._root = e)} ref={e => (this._root = e)}
{...props} {...props}
source={resolvedSource} source={resolvedSource}
onFastImageProgress={onProgress}
onFastImageError={onError} onFastImageError={onError}
onFastImageLoad={onLoad} onFastImageLoad={onLoad}
/> />
@ -70,18 +80,24 @@ const FastImageSourcePropType = PropTypes.shape({
FastImage.propTypes = { FastImage.propTypes = {
...View.propTypes, ...View.propTypes,
source: FastImageSourcePropType, source: FastImageSourcePropType,
onFastImageProgress: PropTypes.func,
onFastImageError: PropTypes.func, onFastImageError: PropTypes.func,
onFastImageLoad: PropTypes.func, onFastImageLoad: PropTypes.func,
} }
FastImage.defaultProps = { FastImage.defaultProps = {
resizeMode: FastImage.resizeMode.cover, resizeMode: FastImage.resizeMode.cover,
onProgress: Function.prototype,
onLoad: Function.prototype, onLoad: Function.prototype,
onError: Function.prototype, onError: Function.prototype,
} }
const FastImageView = requireNativeComponent('FastImageView', FastImage, { const FastImageView = requireNativeComponent('FastImageView', FastImage, {
nativeOnly: { onFastImageError: true, onFastImageLoad: true }, nativeOnly: {
onFastImageProgress: true,
onFastImageError: true,
onFastImageLoad: true
},
}) })
export default FastImage export default FastImage

View File

@ -1,6 +1,13 @@
// @flow // @flow
import React, { Component } from 'react' import React, { Component } from 'react'
import { View, Text, StyleSheet, ScrollView, StatusBar } from 'react-native' import {
PixelRatio,
ScrollView,
StatusBar,
StyleSheet,
Text,
View,
} from 'react-native'
import Icon from 'react-native-vector-icons/Ionicons' import Icon from 'react-native-vector-icons/Ionicons'
import FastImage from 'react-native-fast-image' import FastImage from 'react-native-fast-image'
import timeout from 'react-timeout' import timeout from 'react-timeout'
@ -10,6 +17,8 @@ const getImageUrl = (id, width, height) =>
`https://source.unsplash.com/${id}/${width}x${height}` `https://source.unsplash.com/${id}/${width}x${height}`
const IMAGE_SIZE = 150 const IMAGE_SIZE = 150
const IMAGE_SIZE_PX = PixelRatio.getPixelSizeForLayoutSize(IMAGE_SIZE)
// The server is used to test that sending headers is working correctly. // The server is used to test that sending headers is working correctly.
const USE_SERVER = false const USE_SERVER = false
const TOKEN = 'someToken' const TOKEN = 'someToken'
@ -24,8 +33,8 @@ const getImages = () => {
] ]
} }
return [ return [
getImageUrl('x58soEovG_M', IMAGE_SIZE, IMAGE_SIZE), getImageUrl('x58soEovG_M', IMAGE_SIZE_PX, IMAGE_SIZE_PX),
getImageUrl('yPI7myL5eWY', IMAGE_SIZE, IMAGE_SIZE), getImageUrl('yPI7myL5eWY', IMAGE_SIZE_PX, IMAGE_SIZE_PX),
'https://cdn-images-1.medium.com/max/1600/1*-CY5bU4OqiJRox7G00sftw.gif', 'https://cdn-images-1.medium.com/max/1600/1*-CY5bU4OqiJRox7G00sftw.gif',
] ]
} }
@ -81,6 +90,7 @@ class FastImageExample extends Component {
}, },
priority: FastImage.priority.low, priority: FastImage.priority.low,
}} }}
onProgress={e => console.log('progress', e.nativeEvent.progress)}
/> />
<FastImage <FastImage
style={styles.image} style={styles.image}

View File

@ -11,9 +11,11 @@
@interface FFFastImageView : FLAnimatedImageView @interface FFFastImageView : FLAnimatedImageView
@property(nonatomic, copy) RCTDirectEventBlock onFastImageError; @property (nonatomic, copy) RCTBubblingEventBlock onFastImageProgress;
@property(nonatomic, copy) RCTDirectEventBlock onFastImageLoad; @property (nonatomic, copy) RCTDirectEventBlock onFastImageError;
@property(nonatomic, assign) RCTResizeMode resizeMode; @property (nonatomic, copy) RCTDirectEventBlock onFastImageLoad;
@property(nonatomic, strong) FFFastImageSource *source; @property (nonatomic, assign) RCTResizeMode resizeMode;
@property (nonatomic, strong) FFFastImageSource *source;
@end @end

View File

@ -37,21 +37,28 @@
[self sd_setImageWithURL:source.uri [self sd_setImageWithURL:source.uri
placeholderImage:nil placeholderImage:nil
options:options options:options
completed:^(UIImage *image, progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
NSError *error, double progress = MIN(1, MAX(0, (double) receivedSize / (double) expectedSize));
SDImageCacheType cacheType, if (_onFastImageProgress) {
NSURL *imageURL) { _onFastImageProgress(@{ @"progress": @(progress) });
if (error) { }
if (_onFastImageError) {
_onFastImageError(@{}); } completed:^(UIImage * _Nullable image,
} NSError * _Nullable error,
} else { SDImageCacheType cacheType,
if (_onFastImageLoad) { NSURL * _Nullable imageURL) {
_onFastImageLoad(@{}); if (error) {
} if (_onFastImageError) {
} _onFastImageError(@{});
}]; }
} else {
if (_onFastImageLoad) {
_onFastImageLoad(@{});
}
}
}];
} }
} }
@end @end

View File

@ -14,10 +14,11 @@ RCT_EXPORT_MODULE(FastImageView)
return view; return view;
} }
RCT_EXPORT_VIEW_PROPERTY(source, FFFastImageSource); RCT_EXPORT_VIEW_PROPERTY(source, FFFastImageSource)
RCT_EXPORT_VIEW_PROPERTY(resizeMode, RCTResizeMode); RCT_EXPORT_VIEW_PROPERTY(resizeMode, RCTResizeMode)
RCT_EXPORT_VIEW_PROPERTY(onFastImageError, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onFastImageProgress, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onFastImageLoad, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onFastImageError, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onFastImageLoad, RCTDirectEventBlock)
RCT_EXPORT_METHOD(preload:(nonnull NSArray<FFFastImageSource *> *)sources) RCT_EXPORT_METHOD(preload:(nonnull NSArray<FFFastImageSource *> *)sources)
{ {