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';
|
|
|
|
|
2016-06-07 12:29:13 +00:00
|
|
|
const EdgeInsetsPropType = require('EdgeInsetsPropType');
|
|
|
|
const ImageResizeMode = require('ImageResizeMode');
|
|
|
|
const ImageSourcePropType = require('ImageSourcePropType');
|
|
|
|
const ImageStylePropTypes = require('ImageStylePropTypes');
|
2016-11-04 12:40:26 +00:00
|
|
|
const NativeMethodsMixin = require('NativeMethodsMixin');
|
2016-06-07 12:29:13 +00:00
|
|
|
const NativeModules = require('NativeModules');
|
|
|
|
const React = require('React');
|
2017-04-12 23:09:48 +00:00
|
|
|
const PropTypes = require('prop-types');
|
2016-06-07 12:29:13 +00:00
|
|
|
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
|
|
|
|
const StyleSheet = require('StyleSheet');
|
|
|
|
const StyleSheetPropType = require('StyleSheetPropType');
|
|
|
|
|
|
|
|
const flattenStyle = require('flattenStyle');
|
|
|
|
const requireNativeComponent = require('requireNativeComponent');
|
|
|
|
const resolveAssetSource = require('resolveAssetSource');
|
|
|
|
|
|
|
|
const ImageViewManager = NativeModules.ImageViewManager;
|
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
|
|
|
*
|
2017-01-23 23:00:52 +00:00
|
|
|
* This example shows both fetching and displaying an image from local
|
|
|
|
* storage as well as one from network.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2016-06-24 17:39:06 +00:00
|
|
|
* ```ReactNativeWebPlayer
|
|
|
|
* import React, { Component } from 'react';
|
|
|
|
* import { AppRegistry, View, Image } from 'react-native';
|
|
|
|
*
|
2017-05-17 06:43:46 +00:00
|
|
|
* export default class DisplayAnImage extends Component {
|
2016-06-24 17:39:06 +00:00
|
|
|
* render() {
|
|
|
|
* return (
|
|
|
|
* <View>
|
|
|
|
* <Image
|
|
|
|
* source={require('./img/favicon.png')}
|
|
|
|
* />
|
|
|
|
* <Image
|
2016-08-05 15:56:58 +00:00
|
|
|
* style={{width: 50, height: 50}}
|
|
|
|
* source={{uri: 'https://facebook.github.io/react/img/logo_og.png'}}
|
2016-06-24 17:39:06 +00:00
|
|
|
* />
|
|
|
|
* </View>
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2017-05-17 06:43:46 +00:00
|
|
|
* // skip this line if using Create React Native App
|
2016-06-24 17:39:06 +00:00
|
|
|
* AppRegistry.registerComponent('DisplayAnImage', () => DisplayAnImage);
|
2015-03-09 16:28:51 +00:00
|
|
|
* ```
|
2016-06-24 17:39:06 +00:00
|
|
|
*
|
|
|
|
* You can also add `style` to an image:
|
|
|
|
*
|
|
|
|
* ```ReactNativeWebPlayer
|
|
|
|
* import React, { Component } from 'react';
|
2016-11-17 02:06:40 +00:00
|
|
|
* import { AppRegistry, View, Image, StyleSheet } from 'react-native';
|
2016-06-24 17:39:06 +00:00
|
|
|
*
|
|
|
|
* const styles = StyleSheet.create({
|
|
|
|
* stretch: {
|
|
|
|
* width: 50,
|
|
|
|
* height: 200
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
*
|
2017-05-17 06:43:46 +00:00
|
|
|
* export default class DisplayAnImageWithStyle extends Component {
|
2016-06-24 17:39:06 +00:00
|
|
|
* render() {
|
|
|
|
* return (
|
|
|
|
* <View>
|
|
|
|
* <Image
|
|
|
|
* style={styles.stretch}
|
|
|
|
* source={require('./img/favicon.png')}
|
|
|
|
* />
|
|
|
|
* </View>
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2017-05-17 06:43:46 +00:00
|
|
|
* // skip these lines if using Create React Native App
|
2016-06-24 17:39:06 +00:00
|
|
|
* AppRegistry.registerComponent(
|
|
|
|
* 'DisplayAnImageWithStyle',
|
|
|
|
* () => DisplayAnImageWithStyle
|
|
|
|
* );
|
2015-03-09 16:28:51 +00:00
|
|
|
* ```
|
2016-07-31 14:58:56 +00:00
|
|
|
*
|
|
|
|
* ### GIF and WebP support on Android
|
|
|
|
*
|
2017-05-17 06:43:46 +00:00
|
|
|
* When building your own native code, GIF and WebP are not supported by default on Android.
|
2016-07-31 14:58:56 +00:00
|
|
|
*
|
|
|
|
* You will need to add some optional modules in `android/app/build.gradle`, depending on the needs of your app.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* dependencies {
|
|
|
|
* // If your app supports Android versions before Ice Cream Sandwich (API level 14)
|
2017-04-19 11:49:23 +00:00
|
|
|
* compile 'com.facebook.fresco:animated-base-support:1.0.1'
|
2016-07-31 14:58:56 +00:00
|
|
|
*
|
|
|
|
* // For animated GIF support
|
2017-04-19 11:49:23 +00:00
|
|
|
* compile 'com.facebook.fresco:animated-gif:1.0.1'
|
2016-07-31 14:58:56 +00:00
|
|
|
*
|
|
|
|
* // For WebP support, including animated WebP
|
2017-04-19 11:49:23 +00:00
|
|
|
* compile 'com.facebook.fresco:animated-webp:1.0.1'
|
|
|
|
* compile 'com.facebook.fresco:webpsupport:1.0.1'
|
2016-07-31 14:58:56 +00:00
|
|
|
*
|
|
|
|
* // For WebP support, without animations
|
2017-04-19 11:49:23 +00:00
|
|
|
* compile 'com.facebook.fresco:webpsupport:1.0.1'
|
2016-07-31 14:58:56 +00:00
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Also, if you use GIF with ProGuard, you will need to add this rule in `proguard-rules.pro` :
|
|
|
|
* ```
|
|
|
|
* -keep class com.facebook.imagepipeline.animated.factory.AnimatedFactoryImpl {
|
|
|
|
* public AnimatedFactoryImpl(com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory, com.facebook.imagepipeline.core.ExecutorSupplier);
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2016-06-07 12:29:13 +00:00
|
|
|
const Image = React.createClass({
|
2015-02-20 04:10:52 +00:00
|
|
|
propTypes: {
|
2016-06-24 17:39:06 +00:00
|
|
|
/**
|
|
|
|
* > `ImageResizeMode` is an `Enum` for different image resizing modes, set via the
|
|
|
|
* > `resizeMode` style property on `Image` components. The values are `contain`, `cover`,
|
|
|
|
* > `stretch`, `center`, `repeat`.
|
|
|
|
*/
|
2015-07-15 20:17:13 +00:00
|
|
|
style: StyleSheetPropType(ImageStylePropTypes),
|
2015-04-01 17:56:21 +00:00
|
|
|
/**
|
2016-06-01 17:32:20 +00:00
|
|
|
* The image source (either a remote URL or a local file resource).
|
2016-07-28 20:58:50 +00:00
|
|
|
*
|
|
|
|
* This prop can also contain several remote URLs, specified together with
|
|
|
|
* their width and height and potentially with scale/other URI arguments.
|
|
|
|
* The native side will then choose the best `uri` to display based on the
|
2017-01-18 00:58:27 +00:00
|
|
|
* measured size of the image container. A `cache` property can be added to
|
|
|
|
* control how networked request interacts with the local cache.
|
2017-05-24 19:13:48 +00:00
|
|
|
*
|
|
|
|
* The currently supported formats are `png`, `jpg`, `jpeg`, `bmp`, `gif`,
|
|
|
|
* `webp` (Android only), `psd` (iOS only).
|
2015-04-01 17:56:21 +00:00
|
|
|
*/
|
2016-06-01 17:32:20 +00:00
|
|
|
source: ImageSourcePropType,
|
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.
|
2016-06-24 17:39:06 +00:00
|
|
|
*
|
|
|
|
* - `uri` - a string representing the resource identifier for the image, which
|
|
|
|
* should be either a local file path or the name of a static image resource
|
|
|
|
* (which should be wrapped in the `require('./path/to/image.png')` function).
|
|
|
|
* - `width`, `height` - can be specified if known at build time, in which case
|
|
|
|
* these will be used to set the default `<Image/>` component dimensions.
|
|
|
|
* - `scale` - used to indicate the scale factor of the image. Defaults to 1.0 if
|
|
|
|
* unspecified, meaning that one image pixel equates to one display point / DIP.
|
|
|
|
* - `number` - Opaque type returned by something like `require('./image.jpg')`.
|
|
|
|
*
|
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([
|
2016-06-24 17:39:06 +00:00
|
|
|
// TODO: Tooling to support documenting these directly and having them display in the docs.
|
2015-12-08 11:29:08 +00:00
|
|
|
PropTypes.shape({
|
|
|
|
uri: PropTypes.string,
|
2016-06-01 17:32:20 +00:00
|
|
|
width: PropTypes.number,
|
|
|
|
height: PropTypes.number,
|
|
|
|
scale: PropTypes.number,
|
2015-12-08 11:29:08 +00:00
|
|
|
}),
|
|
|
|
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
|
|
|
*/
|
2017-02-03 06:10:35 +00:00
|
|
|
accessibilityLabel: PropTypes.node,
|
2016-03-17 19:25:07 +00:00
|
|
|
/**
|
|
|
|
* blurRadius: the blur radius of the blur filter added to the image
|
|
|
|
*/
|
|
|
|
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
|
2016-06-24 17:39:06 +00:00
|
|
|
* by `capInsets` will stay a fixed size, but the center content and borders
|
2015-02-20 04:10:52 +00:00
|
|
|
* of the image will be stretched. This is useful for creating resizable
|
2016-06-24 17:39:06 +00:00
|
|
|
* rounded buttons, shadows, and other resizable assets. More info in the
|
|
|
|
* [official 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,
|
2016-09-09 12:00:52 +00:00
|
|
|
/**
|
|
|
|
* The mechanism that should be used to resize the image when the image's dimensions
|
|
|
|
* differ from the image view's dimensions. Defaults to `auto`.
|
|
|
|
*
|
|
|
|
* - `auto`: Use heuristics to pick between `resize` and `scale`.
|
|
|
|
*
|
|
|
|
* - `resize`: A software operation which changes the encoded image in memory before it
|
|
|
|
* gets decoded. This should be used instead of `scale` when the image is much larger
|
|
|
|
* than the view.
|
|
|
|
*
|
|
|
|
* - `scale`: The image gets drawn downscaled or upscaled. Compared to `resize`, `scale` is
|
|
|
|
* faster (usually hardware accelerated) and produces higher quality images. This
|
|
|
|
* should be used if the image is smaller than the view. It should also be used if the
|
|
|
|
* image is slightly bigger than the view.
|
|
|
|
*
|
|
|
|
* More details about `resize` and `scale` can be found at http://frescolib.org/docs/resizing-rotating.html.
|
|
|
|
*
|
|
|
|
* @platform android
|
|
|
|
*/
|
|
|
|
resizeMethod: PropTypes.oneOf(['auto', 'resize', 'scale']),
|
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
|
|
|
*
|
2016-06-24 17:39:06 +00:00
|
|
|
* - `cover`: Scale the image uniformly (maintain the image's aspect ratio)
|
2015-12-16 01:33:37 +00:00
|
|
|
* 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).
|
|
|
|
*
|
2016-06-24 17:39:06 +00:00
|
|
|
* - `contain`: Scale the image uniformly (maintain the image's aspect ratio)
|
2015-12-16 01:33:37 +00:00
|
|
|
* 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).
|
|
|
|
*
|
2016-06-24 17:39:06 +00:00
|
|
|
* - `stretch`: Scale width and height independently, This may change the
|
2015-12-16 01:33:37 +00:00
|
|
|
* aspect ratio of the src.
|
2016-06-22 11:13:22 +00:00
|
|
|
*
|
2016-06-24 17:39:06 +00:00
|
|
|
* - `repeat`: Repeat the image to cover the frame of the view. The
|
2016-06-22 11:13:22 +00:00
|
|
|
* image will keep it's size and aspect ratio. (iOS only)
|
2015-04-17 01:17:19 +00:00
|
|
|
*/
|
2016-07-19 10:33:37 +00:00
|
|
|
resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch', 'repeat', 'center']),
|
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,
|
|
|
|
/**
|
2016-06-24 17:39:06 +00:00
|
|
|
* Invoked on load start.
|
|
|
|
*
|
|
|
|
* e.g., `onLoadStart={(e) => this.setState({loading: true})}`
|
2015-07-09 16:48:22 +00:00
|
|
|
*/
|
|
|
|
onLoadStart: PropTypes.func,
|
|
|
|
/**
|
2016-06-24 17:39:06 +00:00
|
|
|
* Invoked on download progress with `{nativeEvent: {loaded, total}}`.
|
2015-07-24 13:01:33 +00:00
|
|
|
* @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
|
|
|
/**
|
2016-06-24 17:39:06 +00:00
|
|
|
* Invoked on load error with `{nativeEvent: {error}}`.
|
2015-07-09 16:48:22 +00:00
|
|
|
*/
|
2015-07-15 20:17:13 +00:00
|
|
|
onError: PropTypes.func,
|
2016-09-21 19:11:19 +00:00
|
|
|
/**
|
|
|
|
* Invoked when a partial load of the image is complete. The definition of
|
|
|
|
* what constitutes a "partial load" is loader specific though this is meant
|
|
|
|
* for progressive JPEG loads.
|
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
onPartialLoad: PropTypes.func,
|
2015-07-09 16:48:22 +00:00
|
|
|
/**
|
2016-06-24 17:39:06 +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,
|
|
|
|
/**
|
2016-06-24 17:39:06 +00:00
|
|
|
* Invoked when load either succeeds or fails.
|
2015-07-15 20:17:13 +00:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2017-03-08 14:41:17 +00:00
|
|
|
* Does not work for static image resources.
|
|
|
|
*
|
2016-06-24 17:39:06 +00:00
|
|
|
* @param uri The location of the image.
|
2017-01-04 10:37:51 +00:00
|
|
|
* @param success The function that will be called if the image was successfully found and width
|
2016-06-24 17:39:06 +00:00
|
|
|
* and height retrieved.
|
|
|
|
* @param failure The function that will be called if there was an error, such as failing to
|
|
|
|
* to retrieve the image.
|
|
|
|
*
|
|
|
|
* @returns void
|
|
|
|
*
|
2016-01-21 16:22:44 +00:00
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
getSize: function(
|
|
|
|
uri: string,
|
|
|
|
success: (width: number, height: number) => void,
|
2017-04-05 17:16:30 +00:00
|
|
|
failure?: (error: any) => void,
|
2016-01-21 16:22:44 +00:00
|
|
|
) {
|
|
|
|
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
|
2016-06-24 17:39:06 +00:00
|
|
|
*
|
|
|
|
* @param url The remote location of the image.
|
|
|
|
*
|
|
|
|
* @return The prefetched image.
|
2016-04-13 14:29:10 +00:00
|
|
|
*/
|
|
|
|
prefetch(url: string) {
|
2016-06-07 12:29:13 +00:00
|
|
|
return ImageViewManager.prefetchImage(url);
|
2016-04-13 14:29:10 +00:00
|
|
|
},
|
2016-11-15 05:01:02 +00:00
|
|
|
/**
|
|
|
|
* Resolves an asset reference into an object which has the properties `uri`, `width`,
|
|
|
|
* and `height`. The input may either be a number (opaque type returned by
|
|
|
|
* require('./foo.png')) or an `ImageSource` like { uri: '<http location || file path>' }
|
|
|
|
*/
|
|
|
|
resolveAssetSource: resolveAssetSource,
|
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
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2016-06-07 12:29:13 +00:00
|
|
|
const source = resolveAssetSource(this.props.source) || { uri: undefined, width: undefined, height: undefined };
|
2016-06-06 20:20:09 +00:00
|
|
|
|
2016-07-28 20:58:50 +00:00
|
|
|
let sources;
|
|
|
|
let style;
|
|
|
|
if (Array.isArray(source)) {
|
|
|
|
style = flattenStyle([styles.base, this.props.style]) || {};
|
|
|
|
sources = source;
|
|
|
|
} else {
|
|
|
|
const {width, height, uri} = source;
|
|
|
|
style = flattenStyle([{width, height}, styles.base, this.props.style]) || {};
|
|
|
|
sources = [source];
|
|
|
|
|
|
|
|
if (uri === '') {
|
|
|
|
console.warn('source.uri should not be an empty string');
|
|
|
|
}
|
2016-06-01 17:32:20 +00:00
|
|
|
}
|
2016-06-06 20:20:09 +00:00
|
|
|
|
2016-07-28 20:58:50 +00:00
|
|
|
const resizeMode = this.props.resizeMode || (style || {}).resizeMode || 'cover'; // Workaround for flow bug t7737108
|
|
|
|
const tintColor = (style || {}).tintColor; // Workaround for flow bug t7737108
|
|
|
|
|
2016-03-04 15:31:38 +00:00
|
|
|
if (this.props.src) {
|
|
|
|
console.warn('The <Image> component requires a `source` property rather than `src`.');
|
|
|
|
}
|
|
|
|
|
2016-01-22 19:31:40 +00:00
|
|
|
return (
|
2016-06-07 12:29:13 +00:00
|
|
|
<RCTImageView
|
2016-01-22 19:31:40 +00:00
|
|
|
{...this.props}
|
|
|
|
style={style}
|
|
|
|
resizeMode={resizeMode}
|
|
|
|
tintColor={tintColor}
|
2016-07-28 20:58:50 +00:00
|
|
|
source={sources}
|
2016-01-22 19:31:40 +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
|
|
|
});
|
|
|
|
|
2016-06-07 12:29:13 +00:00
|
|
|
const styles = StyleSheet.create({
|
2015-02-20 04:10:52 +00:00
|
|
|
base: {
|
|
|
|
overflow: 'hidden',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-06-07 12:29:13 +00:00
|
|
|
const 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
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
module.exports = Image;
|