mirror of
https://github.com/status-im/react-native.git
synced 2025-01-25 08:48:58 +00:00
61c648d564
Summary: RCTNetworkImageView and RCTStaticImage had significant overlap in functionality, but each had a different subset of features and bugs. This diff merges most of the functionality of RCTNetworkImageView into RCTStaticImage, eliminating some bugs in the former, such as constant redrawing when properties were changed. I've also removed the onLoadAbort event for now (as it wasn't implemented), and renamed the other events to match the web specs for `<img>` and XHMLHttpRequest. The API is essentially what Adobe proposed here: http://blogs.adobe.com/webplatform/2012/01/13/html5-image-progress-events/ The following features have not yet been ported from RCTNetworkImageView: - Background color compositing. It's not clear that this adds much value and it increases memory consumption, etc. - Image request cancelling when images are removed from view. Again, it's not clear if this is a huge benefit, but if it is it should be combined with other optimisations, such as unloading offscreen images. (Note that this only affects the open source fork. For now, internal apps will still use FBNetworkImageView for remote images.)
183 lines
5.5 KiB
JavaScript
183 lines
5.5 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule Image
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
var EdgeInsetsPropType = require('EdgeInsetsPropType');
|
|
var ImageResizeMode = require('ImageResizeMode');
|
|
var ImageStylePropTypes = require('ImageStylePropTypes');
|
|
var NativeMethodsMixin = require('NativeMethodsMixin');
|
|
var NativeModules = require('NativeModules');
|
|
var PropTypes = require('ReactPropTypes');
|
|
var React = require('React');
|
|
var ReactNativeViewAttributes = require('ReactNativeViewAttributes');
|
|
var StyleSheet = require('StyleSheet');
|
|
var StyleSheetPropType = require('StyleSheetPropType');
|
|
|
|
var flattenStyle = require('flattenStyle');
|
|
var invariant = require('invariant');
|
|
var requireNativeComponent = require('requireNativeComponent');
|
|
var resolveAssetSource = require('resolveAssetSource');
|
|
var verifyPropTypes = require('verifyPropTypes');
|
|
var warning = require('warning');
|
|
|
|
/**
|
|
* A React component for displaying different types of images,
|
|
* including network images, static resources, temporary local images, and
|
|
* images from local disk, such as the camera roll.
|
|
*
|
|
* Example usage:
|
|
*
|
|
* ```
|
|
* renderImages: function() {
|
|
* return (
|
|
* <View>
|
|
* <Image
|
|
* style={styles.icon}
|
|
* source={require('image!myIcon')}
|
|
* />
|
|
* <Image
|
|
* style={styles.logo}
|
|
* source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}}
|
|
* />
|
|
* </View>
|
|
* );
|
|
* },
|
|
* ```
|
|
*/
|
|
|
|
var Image = React.createClass({
|
|
propTypes: {
|
|
style: StyleSheetPropType(ImageStylePropTypes),
|
|
/**
|
|
* `uri` is a string representing the resource identifier for the image, which
|
|
* could be an http address, a local file path, or the name of a static image
|
|
* resource (which should be wrapped in the `require('image!name')` function).
|
|
*/
|
|
source: PropTypes.shape({
|
|
uri: PropTypes.string,
|
|
}),
|
|
/**
|
|
* A static image to display while downloading the final image off the
|
|
* network.
|
|
*/
|
|
defaultSource: PropTypes.shape({
|
|
uri: PropTypes.string,
|
|
}),
|
|
/**
|
|
* Whether this element should be revealed as an accessible element.
|
|
*/
|
|
accessible: PropTypes.bool,
|
|
/**
|
|
* Custom string to display for accessibility.
|
|
*/
|
|
accessibilityLabel: PropTypes.string,
|
|
/**
|
|
* When the image is resized, the corners of the size specified
|
|
* by capInsets will stay a fixed size, but the center content and borders
|
|
* of the image will be stretched. This is useful for creating resizable
|
|
* rounded buttons, shadows, and other resizable assets. More info on
|
|
* [Apple documentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/index.html#//apple_ref/occ/instm/UIImage/resizableImageWithCapInsets)
|
|
*/
|
|
capInsets: EdgeInsetsPropType,
|
|
/**
|
|
* Determines how to resize the image when the frame doesn't match the raw
|
|
* image dimensions.
|
|
*/
|
|
resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch']),
|
|
/**
|
|
* A unique identifier for this element to be used in UI Automation
|
|
* testing scripts.
|
|
*/
|
|
testID: PropTypes.string,
|
|
/**
|
|
* Invoked on mount and layout changes with
|
|
*
|
|
* {nativeEvent: {layout: {x, y, width, height}}}.
|
|
*/
|
|
onLayout: PropTypes.func,
|
|
/**
|
|
* Invoked on load start
|
|
*/
|
|
onLoadStart: PropTypes.func,
|
|
/**
|
|
* Invoked on download progress with
|
|
*
|
|
* {nativeEvent: {loaded, total}}.
|
|
*/
|
|
onProgress: PropTypes.func,
|
|
/**
|
|
* Invoked on load error
|
|
*
|
|
* {nativeEvent: {error}}.
|
|
*/
|
|
onError: PropTypes.func,
|
|
/**
|
|
* Invoked when load completes successfully
|
|
*/
|
|
onLoad: PropTypes.func,
|
|
/**
|
|
* Invoked when load either succeeds or fails
|
|
*/
|
|
onLoadEnd: PropTypes.func,
|
|
},
|
|
|
|
statics: {
|
|
resizeMode: ImageResizeMode,
|
|
},
|
|
|
|
mixins: [NativeMethodsMixin],
|
|
|
|
/**
|
|
* `NativeMethodsMixin` will look for this when invoking `setNativeProps`. We
|
|
* make `this` look like an actual native component class.
|
|
*/
|
|
viewConfig: {
|
|
uiViewClassName: 'UIView',
|
|
validAttributes: ReactNativeViewAttributes.UIView
|
|
},
|
|
|
|
render: function() {
|
|
var source = resolveAssetSource(this.props.source) || {};
|
|
var defaultSource = (this.props.defaultSource && resolveAssetSource(this.props.defaultSource)) || {};
|
|
|
|
var {width, height} = source;
|
|
var style = flattenStyle([{width, height}, styles.base, this.props.style]) || {};
|
|
|
|
var isNetwork = source.uri && source.uri.match(/^https?:/);
|
|
var RawImage = isNetwork ? RCTNetworkImageView : RCTImageView;
|
|
var resizeMode = this.props.resizeMode || (style || {}).resizeMode || 'cover'; // Workaround for flow bug t7737108
|
|
var tintColor = (style || {}).tintColor; // Workaround for flow bug t7737108
|
|
|
|
return (
|
|
<RawImage
|
|
{...this.props}
|
|
style={style}
|
|
resizeMode={resizeMode}
|
|
tintColor={tintColor}
|
|
src={source.uri}
|
|
defaultSrc={defaultSource.uri}
|
|
/>
|
|
);
|
|
}
|
|
});
|
|
|
|
var styles = StyleSheet.create({
|
|
base: {
|
|
overflow: 'hidden',
|
|
},
|
|
});
|
|
|
|
var RCTImageView = requireNativeComponent('RCTImageView', null);
|
|
var RCTNetworkImageView = (NativeModules.NetworkImageViewManager) ? requireNativeComponent('RCTNetworkImageView', null) : RCTImageView;
|
|
|
|
module.exports = Image;
|