2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* 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.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* @providesModule Image
|
2015-03-26 17:06:50 +00:00
|
|
|
* @flow
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var EdgeInsetsPropType = require('EdgeInsetsPropType');
|
2015-04-21 17:48:54 +00:00
|
|
|
var ImageResizeMode = require('ImageResizeMode');
|
|
|
|
var ImageStylePropTypes = require('ImageStylePropTypes');
|
2015-02-20 04:10:52 +00:00
|
|
|
var NativeMethodsMixin = require('NativeMethodsMixin');
|
2016-04-13 14:29:10 +00:00
|
|
|
var NativeModules = require('NativeModules');
|
2015-02-20 04:10:52 +00:00
|
|
|
var PropTypes = require('ReactPropTypes');
|
|
|
|
var React = require('React');
|
2015-05-08 16:45:43 +00:00
|
|
|
var ReactNativeViewAttributes = require('ReactNativeViewAttributes');
|
2015-02-20 04:10:52 +00:00
|
|
|
var StyleSheet = require('StyleSheet');
|
|
|
|
var StyleSheetPropType = require('StyleSheetPropType');
|
|
|
|
|
|
|
|
var flattenStyle = require('flattenStyle');
|
2015-04-17 01:17:19 +00:00
|
|
|
var requireNativeComponent = require('requireNativeComponent');
|
2015-04-21 17:48:54 +00:00
|
|
|
var resolveAssetSource = require('resolveAssetSource');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
var {
|
2016-04-13 14:29:10 +00:00
|
|
|
ImageLoader,
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
ImageViewManager,
|
|
|
|
NetworkImageViewManager,
|
2016-04-13 14:29:10 +00:00
|
|
|
} = NativeModules;
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-04-27 20:55:01 +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}
|
2016-01-04 15:40:15 +00:00
|
|
|
* source={require('./myIcon.png')}
|
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-07-15 20:17:13 +00:00
|
|
|
style: StyleSheetPropType(ImageStylePropTypes),
|
2015-04-01 17:56:21 +00:00
|
|
|
/**
|
|
|
|
* `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
|
2016-01-04 15:40:15 +00:00
|
|
|
* resource (which should be wrapped in the `require('./path/to/image.png')` function).
|
2015-04-01 17:56:21 +00:00
|
|
|
*/
|
2015-08-20 04:44:59 +00:00
|
|
|
source: PropTypes.oneOfType([
|
|
|
|
PropTypes.shape({
|
|
|
|
uri: PropTypes.string,
|
|
|
|
}),
|
|
|
|
// Opaque type returned by require('./image.jpg')
|
|
|
|
PropTypes.number,
|
|
|
|
]),
|
2015-04-17 01:17:19 +00:00
|
|
|
/**
|
2015-12-08 11:29:08 +00:00
|
|
|
* A static image to display while loading the image source.
|
2015-07-24 13:01:33 +00:00
|
|
|
* @platform ios
|
2015-04-17 01:17:19 +00:00
|
|
|
*/
|
2015-12-08 11:29:08 +00:00
|
|
|
defaultSource: PropTypes.oneOfType([
|
|
|
|
PropTypes.shape({
|
|
|
|
uri: PropTypes.string,
|
|
|
|
}),
|
|
|
|
// Opaque type returned by require('./image.jpg')
|
|
|
|
PropTypes.number,
|
|
|
|
]),
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-07-24 13:01:33 +00:00
|
|
|
* When true, indicates the image is an accessibility element.
|
|
|
|
* @platform ios
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
accessible: PropTypes.bool,
|
|
|
|
/**
|
2015-07-24 13:01:33 +00:00
|
|
|
* The text that's read by the screen reader when the user interacts with
|
|
|
|
* the image.
|
|
|
|
* @platform ios
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
accessibilityLabel: PropTypes.string,
|
2016-03-17 19:25:07 +00:00
|
|
|
/**
|
|
|
|
* blurRadius: the blur radius of the blur filter added to the image
|
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
blurRadius: PropTypes.number,
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-04-01 17:56:21 +00:00
|
|
|
* When the image is resized, the corners of the size specified
|
2015-02-20 04:10:52 +00:00
|
|
|
* 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
|
2015-04-01 17:56:21 +00:00
|
|
|
* 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)
|
2015-07-24 13:01:33 +00:00
|
|
|
* @platform ios
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
capInsets: EdgeInsetsPropType,
|
2015-04-17 01:17:19 +00:00
|
|
|
/**
|
|
|
|
* Determines how to resize the image when the frame doesn't match the raw
|
|
|
|
* image dimensions.
|
2015-12-16 01:33:37 +00:00
|
|
|
*
|
|
|
|
* 'cover': Scale the image uniformly (maintain the image's aspect ratio)
|
|
|
|
* so that both dimensions (width and height) of the image will be equal
|
|
|
|
* to or larger than the corresponding dimension of the view (minus padding).
|
|
|
|
*
|
|
|
|
* 'contain': Scale the image uniformly (maintain the image's aspect ratio)
|
|
|
|
* so that both dimensions (width and height) of the image will be equal to
|
|
|
|
* or less than the corresponding dimension of the view (minus padding).
|
|
|
|
*
|
|
|
|
* 'stretch': Scale width and height independently, This may change the
|
|
|
|
* aspect ratio of the src.
|
2015-04-17 01:17:19 +00:00
|
|
|
*/
|
|
|
|
resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch']),
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-04-01 17:56:21 +00:00
|
|
|
* A unique identifier for this element to be used in UI Automation
|
2015-02-20 04:10:52 +00:00
|
|
|
* testing scripts.
|
|
|
|
*/
|
|
|
|
testID: PropTypes.string,
|
2015-05-16 01:05:49 +00:00
|
|
|
/**
|
|
|
|
* Invoked on mount and layout changes with
|
2015-07-24 13:01:33 +00:00
|
|
|
* `{nativeEvent: {layout: {x, y, width, height}}}`.
|
2015-05-16 01:05:49 +00:00
|
|
|
*/
|
2015-07-09 16:48:22 +00:00
|
|
|
onLayout: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* Invoked on load start
|
|
|
|
*/
|
|
|
|
onLoadStart: PropTypes.func,
|
|
|
|
/**
|
2015-07-24 13:01:33 +00:00
|
|
|
* Invoked on download progress with `{nativeEvent: {loaded, total}}`
|
|
|
|
* @platform ios
|
2015-07-09 16:48:22 +00:00
|
|
|
*/
|
2015-07-15 20:17:13 +00:00
|
|
|
onProgress: PropTypes.func,
|
2015-07-09 16:48:22 +00:00
|
|
|
/**
|
2015-07-24 13:01:33 +00:00
|
|
|
* Invoked on load error with `{nativeEvent: {error}}`
|
|
|
|
* @platform ios
|
2015-07-09 16:48:22 +00:00
|
|
|
*/
|
2015-07-15 20:17:13 +00:00
|
|
|
onError: PropTypes.func,
|
2015-07-09 16:48:22 +00:00
|
|
|
/**
|
2015-07-15 20:17:13 +00:00
|
|
|
* Invoked when load completes successfully
|
2015-07-09 16:48:22 +00:00
|
|
|
*/
|
2015-07-15 20:17:13 +00:00
|
|
|
onLoad: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* Invoked when load either succeeds or fails
|
|
|
|
*/
|
|
|
|
onLoadEnd: PropTypes.func,
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
statics: {
|
|
|
|
resizeMode: ImageResizeMode,
|
2016-01-21 16:22:44 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the width and height (in pixels) of an image prior to displaying it.
|
|
|
|
* This method can fail if the image cannot be found, or fails to download.
|
|
|
|
*
|
|
|
|
* In order to retrieve the image dimensions, the image may first need to be
|
|
|
|
* loaded or downloaded, after which it will be cached. This means that in
|
|
|
|
* principle you could use this method to preload images, however it is not
|
|
|
|
* optimized for that purpose, and may in future be implemented in a way that
|
|
|
|
* does not fully load/download the image data. A proper, supported way to
|
|
|
|
* preload images will be provided as a separate API.
|
|
|
|
*
|
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
getSize: function(
|
|
|
|
uri: string,
|
|
|
|
success: (width: number, height: number) => void,
|
|
|
|
failure: (error: any) => void,
|
|
|
|
) {
|
|
|
|
ImageViewManager.getSize(uri, success, failure || function() {
|
|
|
|
console.warn('Failed to get size for image: ' + uri);
|
|
|
|
});
|
2016-04-13 14:29:10 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Prefetches a remote image for later use by downloading it to the disk
|
|
|
|
* cache
|
|
|
|
*/
|
|
|
|
prefetch(url: string) {
|
|
|
|
return ImageLoader.prefetchImage(url);
|
|
|
|
},
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
mixins: [NativeMethodsMixin],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `NativeMethodsMixin` will look for this when invoking `setNativeProps`. We
|
|
|
|
* make `this` look like an actual native component class.
|
|
|
|
*/
|
|
|
|
viewConfig: {
|
|
|
|
uiViewClassName: 'UIView',
|
2015-05-08 16:45:43 +00:00
|
|
|
validAttributes: ReactNativeViewAttributes.UIView
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2015-10-08 18:32:11 +00:00
|
|
|
contextTypes: {
|
|
|
|
isInAParentText: React.PropTypes.bool
|
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
render: function() {
|
2015-05-06 22:51:59 +00:00
|
|
|
var source = resolveAssetSource(this.props.source) || {};
|
2016-01-22 19:31:40 +00:00
|
|
|
var {width, height, uri} = source;
|
2015-07-15 20:17:13 +00:00
|
|
|
var style = flattenStyle([{width, height}, styles.base, this.props.style]) || {};
|
2015-04-21 17:48:54 +00:00
|
|
|
|
2016-01-22 19:31:40 +00:00
|
|
|
var isNetwork = uri && uri.match(/^https?:/);
|
2015-07-15 20:17:13 +00:00
|
|
|
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
|
|
|
|
|
2015-09-24 00:25:41 +00:00
|
|
|
// This is a workaround for #8243665. RCTNetworkImageView does not support tintColor
|
|
|
|
// TODO: Remove this hack once we have one image implementation #8389274
|
2016-03-17 19:25:07 +00:00
|
|
|
if (isNetwork && (tintColor || this.props.blurRadius)) {
|
2015-09-24 00:25:41 +00:00
|
|
|
RawImage = RCTImageView;
|
|
|
|
}
|
|
|
|
|
2016-03-04 15:31:38 +00:00
|
|
|
if (this.props.src) {
|
|
|
|
console.warn('The <Image> component requires a `source` property rather than `src`.');
|
|
|
|
}
|
|
|
|
|
2015-10-08 18:32:11 +00:00
|
|
|
if (this.context.isInAParentText) {
|
2016-01-22 19:31:40 +00:00
|
|
|
RawImage = RCTVirtualImage;
|
|
|
|
if (!width || !height) {
|
2016-02-10 03:15:52 +00:00
|
|
|
console.warn('You must specify a width and height for the image %s', uri);
|
2016-01-22 19:31:40 +00:00
|
|
|
}
|
2015-10-08 18:32:11 +00:00
|
|
|
}
|
2016-01-22 19:31:40 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<RawImage
|
|
|
|
{...this.props}
|
|
|
|
style={style}
|
|
|
|
resizeMode={resizeMode}
|
|
|
|
tintColor={tintColor}
|
|
|
|
source={source}
|
|
|
|
/>
|
|
|
|
);
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
},
|
2015-02-20 04:10:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
base: {
|
|
|
|
overflow: 'hidden',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-12-08 11:29:08 +00:00
|
|
|
var RCTImageView = requireNativeComponent('RCTImageView', Image);
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
var RCTNetworkImageView = NetworkImageViewManager ? requireNativeComponent('RCTNetworkImageView', Image) : RCTImageView;
|
2015-10-08 18:32:11 +00:00
|
|
|
var RCTVirtualImage = requireNativeComponent('RCTVirtualImage', Image);
|
2015-02-20 04:10:52 +00:00
|
|
|
|
Added getImageSize method
Summary:
public
This diff adds a `getSize()` method to `Image` to retrieve the width and height of an image prior to displaying it. This is useful when working with images from uncontrolled sources, and has been a much-requested feature.
In order to retrieve the image dimensions, the image may first need to be loaded or downloaded, after which it will be cached. This means that in principle you could use this method to preload images, however it is not optimized for that purpose, and may in future be implemented in a way that does not fully load/download the image data.
A fully supported way to preload images will be provided in a future diff.
The API (separate success and failure callbacks) is far from ideal, but until we agree on a unified standard, this was the most conventional way I could think of to implement it. If it returned a promise or something similar, it would be unique among all such APIS in the framework.
Please note that this has been a long time coming, in part due to much bikeshedding about what the API should look like, so while it's not unlikely that the API may change in future, I think having *some* way to do this is better than waiting until we can define the "perfect" way.
Reviewed By: vjeux
Differential Revision: D2797365
fb-gh-sync-id: 11eb1b8547773b1f8be0bc55ddf6dfedebf7fc0a
2016-01-01 02:50:26 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
module.exports = Image;
|