Skip to content

Commit dcbcda7

Browse files
franticFacebook Github Bot
authored andcommitted
Introduce nativeImageSource API
Summary: The goal is to replace `require('image!...')` with an API that communicates better of what's going on under the hood. Reviewed By: yungsters, fkgozali Differential Revision: D4186241 fbshipit-source-id: b764588dbbd9494dd6905b2346e3274b575a9644
1 parent 15f848e commit dcbcda7

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule nativeImageSource
10+
* @flow
11+
*/
12+
'use strict';
13+
14+
const Platform = require('Platform');
15+
16+
type SourceSpec = {
17+
ios?: string,
18+
android?: string,
19+
20+
// For more details on width and height, see
21+
// http://facebook.github.io/react-native/docs/images.html#why-not-automatically-size-everything
22+
width: number,
23+
height: number,
24+
}
25+
26+
/**
27+
* In hybrid apps, use `nativeImageSource` to access images that are already available
28+
* on the native side, for example in Xcode Asset Catalogs or Android's drawable folder.
29+
*
30+
* However, keep in mind that React Native Packager does not guarantee that the image exists. If
31+
* the image is missing you'll get an empty box. When adding new images your app needs to be
32+
* recompiled.
33+
*
34+
* Prefer Static Image Resources system which provides more guarantees, automates measurements and
35+
* allows adding new images without rebuilding the native app. For more details visit:
36+
*
37+
* http://facebook.github.io/react-native/docs/images.html
38+
*
39+
*/
40+
function nativeImageSource(spec: SourceSpec): Object {
41+
const uri = Platform.select(spec);
42+
if (!uri) {
43+
console.warn(`No image name given for ${Platform.OS}: ${JSON.stringify(spec)}`);
44+
}
45+
46+
return {
47+
uri,
48+
width: spec.width,
49+
height: spec.height,
50+
};
51+
}
52+
53+
module.exports = nativeImageSource;

0 commit comments

Comments
 (0)