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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | renderImages: function () { return ( <View> <Image style={styles.icon} source={require( './myIcon.png' )} /> <Image style={styles.logo} /> </View> ); }, |
Props
onLayout function
Invoked on mount and layout changes with {nativeEvent: {layout: {x, y, width, height}}}
.
onLoad function
Invoked when load completes successfully
onLoadEnd function
Invoked when load either succeeds or fails
onLoadStart function
Invoked on load start
resizeMode enum('cover', 'contain', 'stretch')
Determines how to resize the image when the frame doesn't match the raw image dimensions.
'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.
source ImageSourcePropType
The image source (either a remote URL or a local file resource).
style style
Flexbox...
ShadowPropTypesIOS#style...
Transforms...
backfaceVisibility enum('visible', 'hidden')
backgroundColor color
borderBottomLeftRadius number
borderBottomRightRadius number
borderColor color
borderRadius number
borderTopLeftRadius number
borderTopRightRadius number
borderWidth number
opacity number
overflow enum('visible', 'hidden')
resizeMode Object.keys(ImageResizeMode)
tintColor color
Changes the color of all the non-transparent pixels to the tintColor.
androidoverlayColor string
When the image has rounded corners, specifying an overlayColor will cause the remaining space in the corners to be filled with a solid color. This is useful in cases which are not supported by the Android implementation of rounded corners: - Certain resize modes, such as 'contain' - Animated GIFs
A typical way to use this prop is with images displayed on a solid background and setting the overlayColor
to the same color as the background.
For details of how this works under the hood, see http://frescolib.org/docs/rounded-corners-and-circles.html
testID string
A unique identifier for this element to be used in UI Automation testing scripts.
iosaccessibilityLabel string
The text that's read by the screen reader when the user interacts with the image.
iosaccessible bool
When true, indicates the image is an accessibility element.
iosblurRadius number
blurRadius: the blur radius of the blur filter added to the image
ioscapInsets {top: number, left: number, bottom: number, right: number}
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
iosdefaultSource {uri: string, width: number, height: number, scale: number}, number
A static image to display while loading the image source.
iosonError function
Invoked on load error with {nativeEvent: {error}}
iosonProgress function
Invoked on download progress with {nativeEvent: {loaded, total}}
Methods
static getSize(uri: string, success: (width: number, height: number) => void, failure: (error: any) => void)
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.
static prefetch(url: string)
Prefetches a remote image for later use by downloading it to the disk cache
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | 'use strict' ; var React = require( 'react' ); var ReactNative = require( 'react-native' ); var { Image, Platform, StyleSheet, Text, View, ActivityIndicatorIOS } = ReactNative; var base64Icon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEsAAABLCAQAAACSR7JhAAADtUlEQVR4Ac3YA2Bj6QLH0XPT1Fzbtm29tW3btm3bfLZtv7e2ObZnms7d8Uw098tuetPzrxv8wiISrtVudrG2JXQZ4VOv+qUfmqCGGl1mqLhoA52oZlb0mrjsnhKpgeUNEs91Z0pd1kvihA3ULGVHiQO2narKSHKkEMulm9VgUyE60s1aWoMQUbpZOWE+kaqs4eLEjdIlZTcFZB0ndc1+lhB1lZrIuk5P2aib1NBpZaL+JaOGIt0ls47SKzLC7CqrlGF6RZ09HGoNy1lYl2aRSWL5GuzqWU1KafRdoRp0iOQEiDzgZPnG6DbldcomadViflnl/cL93tOoVbsOLVM2jylvdWjXolWX1hmfZbGR/wjypDjFLSZIRov09BgYmtUqPQPlQrPapecLgTIy0jMgPKtTeob2zWtrGH3xvjUkPCtNg/tm1rjwrMa+mdUkPd3hWbH0jArPGiU9ufCsNNWFZ40wpwn+62/66R2RUtoso1OB34tnLOcy7YB1fUdc9e0q3yru8PGM773vXsuZ5YIZX+5xmHwHGVvlrGPN6ZSiP1smOsMMde40wKv2VmwPPVXNut4sVpUreZiLBHi0qln/VQeI/LTMYXpsJtFiclUN+5HVZazim+Ky+7sAvxWnvjXrJFneVtLWLyPJu9K3cXLWeOlbMTlrIelbMDlrLenrjEQOtIF+fuI9xRp9ZBFp6+b6WT8RrxEpdK64BuvHgDk+vUy+b5hYk6zfyfs051gRoNO1usU12WWRWL73/MMEy9pMi9qIrR4ZpV16Rrvduxazmy1FSvuFXRkqTnE7m2kdb5U8xGjLw/spRr1uTov4uOgQE+0N/DvFrG/Jt7i/FzwxbA9kDanhf2w+t4V97G8lrT7wc08aA2QNUkuTfW/KimT01wdlfK4yEw030VfT0RtZbzjeMprNq8m8tnSTASrTLti64oBNdpmMQm0eEwvfPwRbUBywG5TzjPCsdwk3IeAXjQblLCoXnDVeoAz6SfJNk5TTzytCNZk/POtTSV40NwOFWzw86wNJRpubpXsn60NJFlHeqlYRbslqZm2jnEZ3qcSKgm0kTli3zZVS7y/iivZTweYXJ26Y+RTbV1zh3hYkgyFGSTKPfRVbRqWWVReaxYeSLarYv1Qqsmh1s95S7G+eEWK0f3jYKTbV6bOwepjfhtafsvUsqrQvrGC8YhmnO9cSCk3yuY984F1vesdHYhWJ5FvASlacshUsajFt2mUM9pqzvKGcyNJW0arTKN1GGGzQlH0tXwLDgQTurS8eIQAAAABJRU5ErkJggg==' ; var ImageCapInsetsExample = require( './ImageCapInsetsExample' ); const IMAGE_PREFETCH_URL = 'http://facebook.github.io/origami/public/images/blog-hero.jpg?r=1&t=' + Date.now(); var prefetchTask = Image.prefetch(IMAGE_PREFETCH_URL); var NetworkImageCallbackExample = React.createClass({ getInitialState: function () { return { events: [], startLoadPrefetched: false , mountTime: new Date(), }; }, componentWillMount() { this .setState({mountTime: new Date()}); }, render: function () { var { mountTime } = this .state; return ( <View> <Image source={ this .props.source} style={[styles.base, {overflow: 'visible' }]} onLoadStart={() => this ._loadEventFired(`✔ onLoadStart (+${ new Date() - mountTime}ms)`)} onLoad={() => this ._loadEventFired(`✔ onLoad (+${ new Date() - mountTime}ms)`)} onLoadEnd={() => { this ._loadEventFired(`✔ onLoadEnd (+${ new Date() - mountTime}ms)`); this .setState({startLoadPrefetched: true }, () => { prefetchTask.then(() => { this ._loadEventFired(`✔ Prefetch OK (+${ new Date() - mountTime}ms)`); }, error => { this ._loadEventFired(`✘ Prefetch failed (+${ new Date() - mountTime}ms)`); }); }); }} /> { this .state.startLoadPrefetched ? <Image source={ this .props.prefetchedSource} style={[styles.base, {overflow: 'visible' }]} onLoadStart={() => this ._loadEventFired(`✔ (prefetched) onLoadStart (+${ new Date() - mountTime}ms)`)} onLoad={() => this ._loadEventFired(`✔ (prefetched) onLoad (+${ new Date() - mountTime}ms)`)} onLoadEnd={() => this ._loadEventFired(`✔ (prefetched) onLoadEnd (+${ new Date() - mountTime}ms)`)} /> : null } <Text style={{marginTop: 20}}> { this .state.events.join( '\n' )} </Text> </View> ); }, _loadEventFired(event) { this .setState((state) => { return state.events = [...state.events, event]; }); } }); var NetworkImageExample = React.createClass({ getInitialState: function () { return { error: false , loading: false , progress: 0 }; }, render: function () { var loader = this .state.loading ? <View style={styles.progress}> <Text>{ this .state.progress}%</Text> <ActivityIndicatorIOS style={{marginLeft:5}}/> </View> : null ; return this .state.error ? <Text>{ this .state.error}</Text> : <Image source={ this .props.source} style={[styles.base, {overflow: 'visible' }]} onLoadStart={(e) => this .setState({loading: true })} onError={(e) => this .setState({error: e.nativeEvent.error, loading: false })} onProgress={(e) => this .setState({progress: Math.round(100 * e.nativeEvent.loaded / e.nativeEvent.total)})} onLoad={() => this .setState({loading: false , error: false })}> {loader} </Image>; } }); var ImageSizeExample = React.createClass({ getInitialState: function () { return { width: 0, height: 0, }; }, componentDidMount: function () { Image.getSize( this .props.source.uri, (width, height) => { this .setState({width, height}); }); }, render: function () { return ( <View style={{flexDirection: 'row' }}> <Image style={{ width: 60, height: 60, backgroundColor: 'transparent' , marginRight: 10, }} source={ this .props.source} /> <Text> Actual dimensions:{ '\n' } Width: { this .state.width}, Height: { this .state.height} </Text> </View> ); }, }); exports.displayName = (undefined: ?string); exports.framework = 'React' ; exports.title = '<Image>' ; exports.description = 'Base component for displaying different types of images.' ; exports.examples = [ { title: 'Plain Network Image' , description: 'If the `source` prop `uri` property is prefixed with ' + '"http", then it will be downloaded from the network.' , render: function () { return ( <Image style={styles.base} /> ); }, }, { title: 'Plain Static Image' , description: 'Static assets should be placed in the source code tree, and ' + 'required in the same way as JavaScript modules.' , render: function () { return ( <View style={styles.horizontal}> <Image source={require( './uie_thumb_normal.png' )} style={styles.icon} /> <Image source={require( './uie_thumb_selected.png' )} style={styles.icon} /> <Image source={require( './uie_comment_normal.png' )} style={styles.icon} /> <Image source={require( './uie_comment_highlighted.png' )} style={styles.icon} /> </View> ); }, }, { title: 'Image Loading Events' , render: function () { return ( <NetworkImageCallbackExample source={{uri: 'http://facebook.github.io/origami/public/images/blog-hero.jpg?r=1&t=' + Date.now()}} prefetchedSource={{uri: IMAGE_PREFETCH_URL}}/> ); }, }, { title: 'Error Handler' , render: function () { return ( <NetworkImageExample source={{uri: 'http://TYPO_ERROR_facebook.github.io/react/img/logo_og.png' }} /> ); }, platform: 'ios' , }, { title: 'Image Download Progress' , render: function () { return ( <NetworkImageExample source={{uri: 'http://facebook.github.io/origami/public/images/blog-hero.jpg?r=1' }}/> ); }, platform: 'ios' , }, { title: 'defaultSource' , description: 'Show a placeholder image when a network image is loading' , render: function () { return ( <Image defaultSource={require( './bunny.png' )} style={styles.base} /> ); }, platform: 'ios' , }, { title: 'Border Color' , render: function () { return ( <View style={styles.horizontal}> <Image source={smallImage} style={[ styles.base, styles.background, {borderWidth: 3, borderColor: '#f099f0' } ]} /> </View> ); }, }, { title: 'Border Width' , render: function () { return ( <View style={styles.horizontal}> <Image source={smallImage} style={[ styles.base, styles.background, {borderWidth: 5, borderColor: '#f099f0' } ]} /> </View> ); }, }, { title: 'Border Radius' , render: function () { return ( <View style={styles.horizontal}> <Image style={[styles.base, {borderRadius: 5}]} source={fullImage} /> <Image style={[styles.base, styles.leftMargin, {borderRadius: 19}]} source={fullImage} /> </View> ); }, }, { title: 'Background Color' , render: function () { return ( <View style={styles.horizontal}> <Image source={smallImage} style={styles.base} /> <Image style={[ styles.base, styles.leftMargin, {backgroundColor: 'rgba(0, 0, 100, 0.25)' } ]} source={smallImage} /> <Image style={[styles.base, styles.leftMargin, {backgroundColor: 'red' }]} source={smallImage} /> <Image style={[styles.base, styles.leftMargin, {backgroundColor: 'black' }]} source={smallImage} /> </View> ); }, }, { title: 'Opacity' , render: function () { return ( <View style={styles.horizontal}> <Image style={[styles.base, {opacity: 1}]} source={fullImage} /> <Image style={[styles.base, styles.leftMargin, {opacity: 0.8}]} source={fullImage} /> <Image style={[styles.base, styles.leftMargin, {opacity: 0.6}]} source={fullImage} /> <Image style={[styles.base, styles.leftMargin, {opacity: 0.4}]} source={fullImage} /> <Image style={[styles.base, styles.leftMargin, {opacity: 0.2}]} source={fullImage} /> <Image style={[styles.base, styles.leftMargin, {opacity: 0}]} source={fullImage} /> </View> ); }, }, { title: 'Nesting' , render: function () { return ( <Image style={{width: 60, height: 60, backgroundColor: 'transparent' }} source={fullImage}> <Text style={styles.nestedText}> React </Text> </Image> ); }, }, { title: 'Tint Color' , description: 'The `tintColor` style prop changes all the non-alpha ' + 'pixels to the tint color.' , render: function () { return ( <View> <View style={styles.horizontal}> <Image source={require( './uie_thumb_normal.png' )} style={[styles.icon, {borderRadius: 5, tintColor: '#5ac8fa' }]} /> <Image source={require( './uie_thumb_normal.png' )} style={[styles.icon, styles.leftMargin, {borderRadius: 5, tintColor: '#4cd964' }]} /> <Image source={require( './uie_thumb_normal.png' )} style={[styles.icon, styles.leftMargin, {borderRadius: 5, tintColor: '#ff2d55' }]} /> <Image source={require( './uie_thumb_normal.png' )} style={[styles.icon, styles.leftMargin, {borderRadius: 5, tintColor: '#8e8e93' }]} /> </View> <Text style={styles.sectionText}> It also works with downloaded images: </Text> <View style={styles.horizontal}> <Image source={smallImage} style={[styles.base, {borderRadius: 5, tintColor: '#5ac8fa' }]} /> <Image source={smallImage} style={[styles.base, styles.leftMargin, {borderRadius: 5, tintColor: '#4cd964' }]} /> <Image source={smallImage} style={[styles.base, styles.leftMargin, {borderRadius: 5, tintColor: '#ff2d55' }]} /> <Image source={smallImage} style={[styles.base, styles.leftMargin, {borderRadius: 5, tintColor: '#8e8e93' }]} /> </View> </View> ); }, }, { title: 'Resize Mode' , description: 'The `resizeMode` style prop controls how the image is ' + 'rendered within the frame.' , render: function () { return ( <View> {[smallImage, fullImage].map((image, index) => { return ( <View key={index}> <View style={styles.horizontal}> <View> <Text style={[styles.resizeModeText]}> Contain </Text> <Image style={styles.resizeMode} resizeMode={Image.resizeMode.contain} source={image} /> </View> <View style={styles.leftMargin}> <Text style={[styles.resizeModeText]}> Cover </Text> <Image style={styles.resizeMode} resizeMode={Image.resizeMode.cover} source={image} /> </View> </View> <View style={styles.horizontal}> <View> <Text style={[styles.resizeModeText]}> Stretch </Text> <Image style={styles.resizeMode} resizeMode={Image.resizeMode.stretch} source={image} /> </View> { Platform.OS === 'android' ? <View style={styles.leftMargin}> <Text style={[styles.resizeModeText]}> Center </Text> <Image style={styles.resizeMode} resizeMode={Image.resizeMode.center} source={image} /> </View> : null } </View> </View> ); })} </View> ); }, }, { title: 'Animated GIF' , render: function () { return ( <Image style={styles.gif} source={{uri: 'http://38.media.tumblr.com/9e9bd08c6e2d10561dd1fb4197df4c4e/tumblr_mfqekpMktw1rn90umo1_500.gif' }} /> ); }, platform: 'ios' , }, { title: 'Base64 image' , render: function () { return ( <Image style={styles.base64} source={{uri: base64Icon, scale: 3}} /> ); }, platform: 'ios' , }, { title: 'Cap Insets' , description: '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.' , render: function () { return <ImageCapInsetsExample />; }, platform: 'ios' , }, { title: 'Image Size' , render: function () { return <ImageSizeExample source={fullImage} />; }, }, ]; var styles = StyleSheet.create({ base: { width: 38, height: 38, }, progress: { flex: 1, alignItems: 'center' , flexDirection: 'row' , width: 100 }, leftMargin: { marginLeft: 10, }, background: { backgroundColor: '#222222' }, sectionText: { marginVertical: 6, }, nestedText: { marginLeft: 12, marginTop: 20, backgroundColor: 'transparent' , color: 'white' }, resizeMode: { width: 90, height: 60, borderWidth: 0.5, borderColor: 'black' }, resizeModeText: { fontSize: 11, marginBottom: 3, }, icon: { width: 15, height: 15, }, horizontal: { flexDirection: 'row' , }, gif: { flex: 1, height: 200, }, base64: { flex: 1, height: 50, resizeMode: 'contain' , }, }); |