Skip to content

Commit db7b44e

Browse files
JoelMarceyFacebook Github Bot 2
authored andcommitted
Update Image API
Summary: - Provide runnable examples - Add more details to properties and jsdoc-ify the methods Ref #8203 Closes #8413 Differential Revision: D3482168 Pulled By: caabernathy fbshipit-source-id: 04fce5133317af282cced5850a53858e3f5b72f2
1 parent f66acad commit db7b44e

File tree

1 file changed

+98
-41
lines changed

1 file changed

+98
-41
lines changed

Libraries/Image/Image.ios.js

Lines changed: 98 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,57 +34,99 @@ const ImageViewManager = NativeModules.ImageViewManager;
3434
* including network images, static resources, temporary local images, and
3535
* images from local disk, such as the camera roll.
3636
*
37-
* Example usage:
37+
* This exmaples shows both fetching and displaying an image from local storage as well as on from
38+
* network.
3839
*
40+
* ```ReactNativeWebPlayer
41+
* import React, { Component } from 'react';
42+
* import { AppRegistry, View, Image } from 'react-native';
43+
*
44+
* class DisplayAnImage extends Component {
45+
* render() {
46+
* return (
47+
* <View>
48+
* <Image
49+
* source={require('./img/favicon.png')}
50+
* />
51+
* <Image
52+
* source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}}
53+
* />
54+
* </View>
55+
* );
56+
* }
57+
* }
58+
*
59+
* // App registration and rendering
60+
* AppRegistry.registerComponent('DisplayAnImage', () => DisplayAnImage);
3961
* ```
40-
* renderImages: function() {
41-
* return (
42-
* <View>
43-
* <Image
44-
* style={styles.icon}
45-
* source={require('./myIcon.png')}
46-
* />
47-
* <Image
48-
* style={styles.logo}
49-
* source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}}
50-
* />
51-
* </View>
52-
* );
53-
* },
62+
*
63+
* You can also add `style` to an image:
64+
*
65+
* ```ReactNativeWebPlayer
66+
* import React, { Component } from 'react';
67+
* import { AppRegistry, View, Image, StyleSheet} from 'react-native';
68+
*
69+
* const styles = StyleSheet.create({
70+
* stretch: {
71+
* width: 50,
72+
* height: 200
73+
* }
74+
* });
75+
*
76+
*class DisplayAnImageWithStyle extends Component {
77+
* render() {
78+
* return (
79+
* <View>
80+
* <Image
81+
* style={styles.stretch}
82+
* source={require('./img/favicon.png')}
83+
* />
84+
* </View>
85+
* );
86+
* }
87+
* }
88+
*
89+
* // App registration and rendering
90+
* AppRegistry.registerComponent(
91+
* 'DisplayAnImageWithStyle',
92+
* () => DisplayAnImageWithStyle
93+
* );
5494
* ```
5595
*/
5696
const Image = React.createClass({
5797
propTypes: {
98+
/**
99+
* > `ImageResizeMode` is an `Enum` for different image resizing modes, set via the
100+
* > `resizeMode` style property on `Image` components. The values are `contain`, `cover`,
101+
* > `stretch`, `center`, `repeat`.
102+
*/
58103
style: StyleSheetPropType(ImageStylePropTypes),
59104
/**
60105
* The image source (either a remote URL or a local file resource).
61106
*/
62107
source: ImageSourcePropType,
63108
/**
64109
* A static image to display while loading the image source.
110+
*
111+
* - `uri` - a string representing the resource identifier for the image, which
112+
* should be either a local file path or the name of a static image resource
113+
* (which should be wrapped in the `require('./path/to/image.png')` function).
114+
* - `width`, `height` - can be specified if known at build time, in which case
115+
* these will be used to set the default `<Image/>` component dimensions.
116+
* - `scale` - used to indicate the scale factor of the image. Defaults to 1.0 if
117+
* unspecified, meaning that one image pixel equates to one display point / DIP.
118+
* - `number` - Opaque type returned by something like `require('./image.jpg')`.
119+
*
65120
* @platform ios
66121
*/
67122
defaultSource: PropTypes.oneOfType([
123+
// TODO: Tooling to support documenting these directly and having them display in the docs.
68124
PropTypes.shape({
69-
/**
70-
* `uri` is a string representing the resource identifier for the image, which
71-
* should be either a local file path or the name of a static image resource
72-
* (which should be wrapped in the `require('./path/to/image.png')` function).
73-
*/
74125
uri: PropTypes.string,
75-
/**
76-
* `width` and `height` can be specified if known at build time, in which case
77-
* these will be used to set the default `<Image/>` component dimensions.
78-
*/
79126
width: PropTypes.number,
80127
height: PropTypes.number,
81-
/**
82-
* `scale` is used to indicate the scale factor of the image. Defaults to 1.0 if
83-
* unspecified, meaning that one image pixel equates to one display point / DIP.
84-
*/
85128
scale: PropTypes.number,
86129
}),
87-
// Opaque type returned by require('./image.jpg')
88130
PropTypes.number,
89131
]),
90132
/**
@@ -105,29 +147,30 @@ const Image = React.createClass({
105147
blurRadius: PropTypes.number,
106148
/**
107149
* When the image is resized, the corners of the size specified
108-
* by capInsets will stay a fixed size, but the center content and borders
150+
* by `capInsets` will stay a fixed size, but the center content and borders
109151
* of the image will be stretched. This is useful for creating resizable
110-
* rounded buttons, shadows, and other resizable assets. More info on
111-
* [Apple documentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/index.html#//apple_ref/occ/instm/UIImage/resizableImageWithCapInsets)
152+
* rounded buttons, shadows, and other resizable assets. More info in the
153+
* [official Apple documentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/index.html#//apple_ref/occ/instm/UIImage/resizableImageWithCapInsets).
154+
*
112155
* @platform ios
113156
*/
114157
capInsets: EdgeInsetsPropType,
115158
/**
116159
* Determines how to resize the image when the frame doesn't match the raw
117160
* image dimensions.
118161
*
119-
* 'cover': Scale the image uniformly (maintain the image's aspect ratio)
162+
* - `cover`: Scale the image uniformly (maintain the image's aspect ratio)
120163
* so that both dimensions (width and height) of the image will be equal
121164
* to or larger than the corresponding dimension of the view (minus padding).
122165
*
123-
* 'contain': Scale the image uniformly (maintain the image's aspect ratio)
166+
* - `contain`: Scale the image uniformly (maintain the image's aspect ratio)
124167
* so that both dimensions (width and height) of the image will be equal to
125168
* or less than the corresponding dimension of the view (minus padding).
126169
*
127-
* 'stretch': Scale width and height independently, This may change the
170+
* - `stretch`: Scale width and height independently, This may change the
128171
* aspect ratio of the src.
129172
*
130-
* 'repeat': Repeat the image to cover the frame of the view. The
173+
* - `repeat`: Repeat the image to cover the frame of the view. The
131174
* image will keep it's size and aspect ratio. (iOS only)
132175
*/
133176
resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch', 'repeat']),
@@ -142,25 +185,27 @@ const Image = React.createClass({
142185
*/
143186
onLayout: PropTypes.func,
144187
/**
145-
* Invoked on load start
188+
* Invoked on load start.
189+
*
190+
* e.g., `onLoadStart={(e) => this.setState({loading: true})}`
146191
*/
147192
onLoadStart: PropTypes.func,
148193
/**
149-
* Invoked on download progress with `{nativeEvent: {loaded, total}}`
194+
* Invoked on download progress with `{nativeEvent: {loaded, total}}`.
150195
* @platform ios
151196
*/
152197
onProgress: PropTypes.func,
153198
/**
154-
* Invoked on load error with `{nativeEvent: {error}}`
199+
* Invoked on load error with `{nativeEvent: {error}}`.
155200
* @platform ios
156201
*/
157202
onError: PropTypes.func,
158203
/**
159-
* Invoked when load completes successfully
204+
* Invoked when load completes successfully.
160205
*/
161206
onLoad: PropTypes.func,
162207
/**
163-
* Invoked when load either succeeds or fails
208+
* Invoked when load either succeeds or fails.
164209
*/
165210
onLoadEnd: PropTypes.func,
166211
},
@@ -178,6 +223,14 @@ const Image = React.createClass({
178223
* does not fully load/download the image data. A proper, supported way to
179224
* preload images will be provided as a separate API.
180225
*
226+
* @param uri The location of the image.
227+
* @param success The function that will be called if the image was sucessfully found and width
228+
* and height retrieved.
229+
* @param failure The function that will be called if there was an error, such as failing to
230+
* to retrieve the image.
231+
*
232+
* @returns void
233+
*
181234
* @platform ios
182235
*/
183236
getSize: function(
@@ -192,6 +245,10 @@ const Image = React.createClass({
192245
/**
193246
* Prefetches a remote image for later use by downloading it to the disk
194247
* cache
248+
*
249+
* @param url The remote location of the image.
250+
*
251+
* @return The prefetched image.
195252
*/
196253
prefetch(url: string) {
197254
return ImageViewManager.prefetchImage(url);

0 commit comments

Comments
 (0)