forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically scale iOS assets based on the target dimensions specifi…
…ed in the style property of the <Image... /> tag. Currently, documentation under "Image > Best Camera Roll Image" is misleading: “iOS saves multiple sizes for the same image in your Camera Roll, it is very important to pick the one that's as close as possible for performance reasons. You wouldn't want to use the full quality 3264x2448 image as source when displaying a 200x200 thumbnail. If there's an exact match, React Native will pick it, otherwise it's going to use the first one that's at least 50% bigger in order to avoid blur when resizing from a close size. All of this is done by default so you don't have to worry about writing the tedious (and error prone) code to do it yourself.” https://facebook.github.io/react-native/docs/image.html This does not occur. Instead, React Native loads the full resolution image no matter the desired size. This means an 8MP photo will require 32MB of memory to display even if displayed in a 100x100 thumbnail. Loading a series of large images will spike memory and likely crash your app. This commit resolves the discrepancy and brings the codebase inline with current documentation. Example: Consider a 3264x2448 image loaded on an iPhone 6 with the following tag: <Image src={{ uri: 'assets-library://... }} style={{ width: 320, height: 240 }}/> The image will automatically be scaled to 640x320 (320x240 target dimensions * a Retina scale of 2.0). This uses considerably less memory than rendering the full resolution asset. It also happens automatically. To force the loading of full resolution, the assetUseMaximumSize option can be passed in: <Image src={{ uri: 'assets-library://...', options: { assetUseMaximumSize: true } }} style={{ width: 320, height: 240 }}/> Additionally, RCTImageLoader has been updated to handle automatic scaling for images from both the Assets Library and Photos Framework. Added an example to UIExplorer. Tap an image in the <CameraRollView>. Issues touched: facebook#1567 facebook#1845 facebook#1579 facebook#930 Note: Pull Request facebook#1704 (facebook#1704) takes an alternate approach by allowing an assetThumbnail prop (boolean) to be passed in to the Image tag's source hash. IE: <Image src={{ uri: ..., assetThumbnail: true }} /> If true, the asset's thumbnail representation will be used. The resolution of this thumbnail is non-configurable and does not scale well.
- Loading branch information
Showing
9 changed files
with
315 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* The examples provided by Facebook are for non-commercial testing and | ||
* evaluation purposes only. | ||
* | ||
* Facebook reserves all rights not expressly granted. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL | ||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
var React = require('react-native'); | ||
var { | ||
Image, | ||
StyleSheet, | ||
Text, | ||
View, | ||
ScrollView | ||
} = React; | ||
|
||
var AssetThumbnailExampleView = React.createClass({ | ||
|
||
getInitialState() { | ||
return { | ||
asset: this.props.asset | ||
}; | ||
}, | ||
|
||
render() { | ||
var asset = this.state.asset; | ||
return ( | ||
<ScrollView> | ||
<View style={ styles.row }> | ||
<Image | ||
source={{ uri: asset.node.image.uri }} | ||
style={ styles.image1 } | ||
/> | ||
</View> | ||
|
||
<View style={ styles.row }> | ||
<Image | ||
source={{ uri: asset.node.image.uri }} | ||
style={ styles.image2 } | ||
/> | ||
</View> | ||
|
||
<View style={ styles.row }> | ||
<Image | ||
source={{ uri: asset.node.image.uri }} | ||
style={ styles.image3 } | ||
/> | ||
</View> | ||
|
||
|
||
|
||
</ScrollView> | ||
); | ||
}, | ||
|
||
|
||
}); | ||
|
||
var styles = StyleSheet.create({ | ||
row: { | ||
padding: 10, | ||
flex: 1, | ||
flexDirection: 'row', | ||
}, | ||
|
||
details: { | ||
margin: 5 | ||
}, | ||
|
||
textColumn: { | ||
flex: 1, | ||
flexDirection: 'column' | ||
}, | ||
|
||
image1: { | ||
borderWidth: 1, | ||
borderColor: 'black', | ||
width: 240, | ||
height: 320, | ||
margin: 5, | ||
}, | ||
|
||
image2: { | ||
borderWidth: 1, | ||
borderColor: 'black', | ||
width: 320, | ||
height: 240 | ||
}, | ||
|
||
image3: { | ||
borderWidth: 1, | ||
borderColor: 'black', | ||
width: 100, | ||
height: 100 | ||
}, | ||
|
||
image4: { | ||
borderWidth: 1, | ||
borderColor: 'black', | ||
width: 200, | ||
height: 200 | ||
}, | ||
|
||
image5: { | ||
borderWidth: 1, | ||
borderColor: 'black', | ||
width: 355, | ||
height: 100 | ||
}, | ||
|
||
image6: { | ||
borderWidth: 1, | ||
borderColor: 'black', | ||
width: 355, | ||
height: 355 | ||
}, | ||
|
||
}); | ||
|
||
exports.title = '<AssetThumbnailExample>'; | ||
exports.description = 'Example component that displays the thumbnail capabilities of the <Image /> tag'; | ||
module.exports = AssetThumbnailExampleView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.