2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* @providesModule Image
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var EdgeInsetsPropType = require('EdgeInsetsPropType');
|
|
|
|
var NativeMethodsMixin = require('NativeMethodsMixin');
|
2015-03-17 10:08:52 +00:00
|
|
|
var NativeModules = require('NativeModules');
|
2015-02-20 04:10:52 +00:00
|
|
|
var PropTypes = require('ReactPropTypes');
|
|
|
|
var ImageResizeMode = require('ImageResizeMode');
|
|
|
|
var ImageStylePropTypes = require('ImageStylePropTypes');
|
|
|
|
var React = require('React');
|
|
|
|
var ReactIOSViewAttributes = require('ReactIOSViewAttributes');
|
|
|
|
var StyleSheet = require('StyleSheet');
|
|
|
|
var StyleSheetPropType = require('StyleSheetPropType');
|
|
|
|
|
|
|
|
var createReactIOSNativeComponentClass = require('createReactIOSNativeComponentClass');
|
|
|
|
var flattenStyle = require('flattenStyle');
|
|
|
|
var insetsDiffer = require('insetsDiffer');
|
|
|
|
var invariant = require('invariant');
|
|
|
|
var merge = require('merge');
|
|
|
|
var warning = require('warning');
|
|
|
|
|
|
|
|
/**
|
2015-03-09 16:28:51 +00:00
|
|
|
* A react component for displaying different types of images,
|
2015-02-20 04:10:52 +00:00
|
|
|
* including network images, static resources, temporary local images, and
|
2015-03-09 16:28:51 +00:00
|
|
|
* images from local disk, such as the camera roll.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-03-09 16:28:51 +00:00
|
|
|
* Example usage:
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-03-09 16:28:51 +00:00
|
|
|
* ```
|
|
|
|
* renderImages: function() {
|
|
|
|
* return (
|
|
|
|
* <View>
|
|
|
|
* <Image
|
|
|
|
* style={styles.icon}
|
2015-03-20 22:51:11 +00:00
|
|
|
* source={require('image!myIcon')}
|
2015-03-09 16:28:51 +00:00
|
|
|
* />
|
|
|
|
* <Image
|
|
|
|
* style={styles.logo}
|
|
|
|
* source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}}
|
|
|
|
* />
|
|
|
|
* </View>
|
|
|
|
* );
|
|
|
|
* },
|
|
|
|
* ```
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
var Image = React.createClass({
|
|
|
|
propTypes: {
|
2015-03-09 16:29:16 +00:00
|
|
|
source: PropTypes.shape({
|
|
|
|
/**
|
|
|
|
* 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 `ix` function).
|
|
|
|
*/
|
|
|
|
uri: PropTypes.string,
|
2015-03-11 21:48:22 +00:00
|
|
|
}),
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* accessible - Whether this element should be revealed as an accessible
|
|
|
|
* element.
|
|
|
|
*/
|
|
|
|
accessible: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* accessibilityLabel - Custom string to display for accessibility.
|
|
|
|
*/
|
|
|
|
accessibilityLabel: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* capInsets - 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:
|
|
|
|
*
|
|
|
|
* https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/index.html#//apple_ref/occ/instm/UIImage/resizableImageWithCapInsets:
|
|
|
|
*/
|
|
|
|
capInsets: EdgeInsetsPropType,
|
|
|
|
style: StyleSheetPropType(ImageStylePropTypes),
|
|
|
|
/**
|
|
|
|
* testID - A unique identifier for this element to be used in UI Automation
|
|
|
|
* testing scripts.
|
|
|
|
*/
|
|
|
|
testID: PropTypes.string,
|
|
|
|
},
|
|
|
|
|
|
|
|
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: ReactIOSViewAttributes.UIView
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var style = flattenStyle([styles.base, this.props.style]);
|
|
|
|
var source = this.props.source;
|
|
|
|
var isNetwork = source.uri && source.uri.match(/^https?:/);
|
|
|
|
invariant(
|
|
|
|
!(isNetwork && source.isStatic),
|
|
|
|
'static image uris cannot start with "http": "' + source.uri + '"'
|
|
|
|
);
|
|
|
|
var isStored = !source.isStatic && !isNetwork;
|
2015-03-17 10:08:46 +00:00
|
|
|
var RawImage = isNetwork ? RCTNetworkImage : RCTStaticImage;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
if (this.props.style && this.props.style.tintColor) {
|
2015-03-17 10:08:46 +00:00
|
|
|
warning(RawImage === RCTStaticImage, 'tintColor style only supported on static images.');
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 05:22:03 +00:00
|
|
|
var contentModes = NativeModules.UIManager.UIView.ContentMode;
|
2015-02-20 04:10:52 +00:00
|
|
|
var resizeMode;
|
|
|
|
if (style.resizeMode === ImageResizeMode.stretch) {
|
|
|
|
resizeMode = contentModes.ScaleToFill;
|
|
|
|
} else if (style.resizeMode === ImageResizeMode.contain) {
|
|
|
|
resizeMode = contentModes.ScaleAspectFit;
|
|
|
|
} else { // ImageResizeMode.cover or undefined
|
|
|
|
resizeMode = contentModes.ScaleAspectFill;
|
|
|
|
}
|
|
|
|
|
|
|
|
var nativeProps = merge(this.props, {
|
|
|
|
style,
|
|
|
|
resizeMode,
|
|
|
|
tintColor: style.tintColor,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isStored) {
|
|
|
|
nativeProps.imageTag = source.uri;
|
|
|
|
} else {
|
|
|
|
nativeProps.src = source.uri;
|
|
|
|
}
|
|
|
|
return <RawImage {...nativeProps} />;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
base: {
|
|
|
|
overflow: 'hidden',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var CommonImageViewAttributes = merge(ReactIOSViewAttributes.UIView, {
|
|
|
|
accessible: true,
|
|
|
|
accessibilityLabel: true,
|
|
|
|
capInsets: {diff: insetsDiffer}, // UIEdgeInsets=UIEdgeInsetsZero
|
|
|
|
imageTag: true,
|
|
|
|
resizeMode: true,
|
|
|
|
src: true,
|
|
|
|
testID: PropTypes.string,
|
|
|
|
});
|
|
|
|
|
2015-03-17 10:08:46 +00:00
|
|
|
var RCTStaticImage = createReactIOSNativeComponentClass({
|
2015-02-20 04:10:52 +00:00
|
|
|
validAttributes: merge(CommonImageViewAttributes, { tintColor: true }),
|
|
|
|
uiViewClassName: 'RCTStaticImage',
|
|
|
|
});
|
|
|
|
|
2015-03-17 10:08:46 +00:00
|
|
|
var RCTNetworkImage = createReactIOSNativeComponentClass({
|
2015-02-20 04:10:52 +00:00
|
|
|
validAttributes: merge(CommonImageViewAttributes, { defaultImageSrc: true }),
|
|
|
|
uiViewClassName: 'RCTNetworkImageView',
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Image;
|