2015-02-20 04:10:52 +00:00
/ * *
2015-03-23 20:35:08 +00:00
* Copyright ( c ) 2015 - present , Facebook , Inc .
*
2018-02-17 02:24:55 +00:00
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree .
2015-02-20 04:10:52 +00:00
*
* @ providesModule Image
2015-03-26 17:06:50 +00:00
* @ flow
2018-01-15 03:32:26 +00:00
* @ format
2015-02-20 04:10:52 +00:00
* /
'use strict' ;
2016-06-07 12:29:13 +00:00
const EdgeInsetsPropType = require ( 'EdgeInsetsPropType' ) ;
2018-03-14 21:04:19 +00:00
const ImageProps = require ( 'ImageProps' ) ;
2016-06-07 12:29:13 +00:00
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' ) ;
2017-07-07 21:24:25 +00:00
const createReactClass = require ( 'create-react-class' ) ;
2016-06-07 12:29:13 +00:00
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
*
2018-01-30 00:10:49 +00:00
* See https : //facebook.github.io/react-native/docs/image.html
2015-02-20 04:10:52 +00:00
* /
2017-07-07 21:24:25 +00:00
const Image = createReactClass ( {
displayName : 'Image' ,
2018-03-14 21:04:19 +00:00
propTypes : ImageProps ,
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 .
*
2018-01-30 00:10:49 +00:00
* See https : //facebook.github.io/react-native/docs/image.html#getsize
2016-01-21 16:22:44 +00:00
* /
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
) {
2018-01-15 03:32:26 +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
2018-01-30 00:10:49 +00:00
* cache .
2016-06-24 17:39:06 +00:00
*
2018-01-30 00:10:49 +00:00
* See https : //facebook.github.io/react-native/docs/image.html#prefetch
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
/ * *
2018-01-30 00:10:49 +00:00
* Resolves an asset reference into an object .
*
* See https : //facebook.github.io/react-native/docs/image.html#resolveassetsource
2016-11-15 05:01:02 +00:00
* /
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' ,
2018-01-15 03:32:26 +00:00
validAttributes : ReactNativeViewAttributes . UIView ,
2015-02-20 04:10:52 +00:00
} ,
render : function ( ) {
2018-01-15 03:32:26 +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 ;
2017-10-25 15:04:39 +00:00
let style ;
2016-07-28 20:58:50 +00:00
if ( Array . isArray ( source ) ) {
2017-10-25 15:04:39 +00:00
style = flattenStyle ( [ styles . base , this . props . style ] ) || { } ;
2016-07-28 20:58:50 +00:00
sources = source ;
} else {
2017-10-25 15:04:39 +00:00
const { width , height , uri } = source ;
2018-01-15 03:32:26 +00:00
style =
flattenStyle ( [ { width , height } , styles . base , this . props . style ] ) || { } ;
2016-07-28 20:58:50 +00:00
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
2018-01-15 03:32:26 +00:00
const resizeMode =
this . props . resizeMode || ( style || { } ) . resizeMode || 'cover' ; // Workaround for flow bug t7737108
2016-07-28 20:58:50 +00:00
const tintColor = ( style || { } ) . tintColor ; // Workaround for flow bug t7737108
2016-03-04 15:31:38 +00:00
if ( this . props . src ) {
2018-01-15 03:32:26 +00:00
console . warn (
'The <Image> component requires a `source` property rather than `src`.' ,
) ;
2016-03-04 15:31:38 +00:00
}
2017-09-26 04:55:56 +00:00
if ( this . props . children ) {
2018-01-15 03:32:26 +00:00
throw new Error (
'The <Image> component cannot contain children. If you want to render content on top of the image, consider using the <ImageBackground> component or absolute positioning.' ,
) ;
2017-09-26 04:55:56 +00:00
}
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 ;