{"id":489,"date":"2023-02-25T14:07:58","date_gmt":"2023-02-25T06:07:58","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=489"},"modified":"2023-04-29T20:51:13","modified_gmt":"2023-04-29T12:51:13","slug":"react-native-learning-image-component","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-learning-image-component\/","title":{"rendered":"React Native\u5b66\u4e60\u4e4b\u56fe\u7247Image\u7ec4\u4ef6"},"content":{"rendered":"<p>React Native\u7684Image\u7ec4\u4ef6\u8c03\u7528\u7684\u56fe\u7247\u9014\u5f84\u6bd4\u8f83\u591a\uff1a\u7f51\u7edc\u56fe\u7247\u3001\u672c\u5730\u56fe\u7247\u3001\u7167\u76f8\u673a\u56fe\u7247<\/p>\n<h2>Image\u7ec4\u4ef6\u7684\u5c5e\u6027\uff1a<\/h2>\n<p>resizeMode\uff1a\u56fe\u7247\u9002\u7528\u7684\u6a21\u5f0f<code>cover<\/code>\u3001<code>contain<\/code>\u3001<code>stretch<\/code><\/p>\n<p><!-- more --><\/p>\n<p>source\uff1a\u56fe\u7247\u7684\u5f15\u7528\u5730\u5740<\/p>\n<ul>\n<li>\u7f51\u7edc\u56fe\u7247\uff1a<code>{% raw %}source={{uri: &#039;http:\/\/www.appblog.cn\/logo.png&#039;}}{% endraw %}<\/code><\/li>\n<li>\u672c\u5730\u56fe\u7247\uff1a<code>{% raw %}source={require(&#039;.\/appnlog.png&#039;)}{% endraw %}<\/code><\/li>\n<\/ul>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u7f51\u7edc\u56fe\u7247\uff0c\u4f60\u9700\u8981\u624b\u52a8\u6307\u5b9a\u56fe\u7247\u7684\u5c3a\u5bf8\uff01<\/p>\n<p>\u5173\u4e8e\u56fe\u7247\u7684\u5c3a\u5bf8\uff0cReact Native\u4f1a\u81ea\u52a8\u8fdb\u884c\u9009\u62e9\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5219\u4f1a\u9009\u62e9\u6700\u63a5\u8fd1\u7684\u5c3a\u5bf8\u8fdb\u884c\u7f29\u653e\uff0c\u4f46\u4e5f\u81f3\u5c11\u7f29\u653e\u5230\u6bd4\u6240\u9700\u5c3a\u5bf8\u5927\u51fa50%\uff0c\u4ee5\u4f7f\u56fe\u7247\u770b\u8d77\u6765\u4ecd\u7136\u8db3\u591f\u6e05\u6670\u3002\u8fd9\u4e00\u5207\u8fc7\u7a0b\u90fd\u662f\u81ea\u52a8\u5b8c\u6210\u7684\uff0c\u6240\u4ee5\u4f60\u4e0d\u7528\u64cd\u5fc3\u81ea\u5df1\u53bb\u5b8c\u6210\u8fd9\u4e9b\u7e41\u7410\u4e14\u6613\u9519\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u4f60\u5728\u6dfb\u52a0\u9759\u6001\u56fe\u7247\u8d44\u6e90\u7684\u65f6\u5019packager\u6b63\u5728\u8fd0\u884c\uff0c\u5219\u4f60\u9700\u8981\u91cd\u542fpackager\u4ee5\u4fbf\u80fd\u6b63\u786e\u5f15\u5165\u65b0\u6dfb\u52a0\u7684\u56fe\u7247\u3002React Native\u6700\u65b0\u7248\u672c\u5df2\u7ecf\u89e3\u51b3\u8be5\u95ee\u9898\u3002<\/p>\n<\/blockquote>\n<h2>React Native \u56fe\u7247\u7ec4\u4ef6\u5904\u7406\u7684\u4f18\u52bf:<\/h2>\n<ul>\n<li>iOS\u548cAndroid\u4e00\u81f4\u7684\u6587\u4ef6\u7cfb\u7edf\u3002<\/li>\n<li>\u56fe\u7247\u548cJS\u4ee3\u7801\u5904\u5728\u76f8\u540c\u7684\u6587\u4ef6\u5939\uff0c\u7ec4\u4ef6\u53ef\u4ee5\u76f4\u63a5\u5305\u542b\u81ea\u5df1\u6240\u7528\u7684\u56fe\u7247\u800c\u4e0d\u7528\u5355\u72ec\u53bb\u8bbe\u7f6e\u3002<\/li>\n<li>\u4e0d\u9700\u8981\u5168\u5c40\u547d\u540d\uff0c\u4e0d\u7528\u62c5\u5fc3\u56fe\u7247\u540d\u5b57\u7684\u51b2\u7a81\u95ee\u9898\u3002<\/li>\n<li>\u53ea\u6709\u5b9e\u9645\u88ab\u7528\u5230\uff08\u5373\u88abrequire\uff09\u7684\u56fe\u7247\u624d\u4f1a\u88ab\u6253\u5305\u5230App\u4e2d\u3002<\/li>\n<li>\u5728\u5f00\u53d1\u671f\u95f4\uff0c\u589e\u52a0\u548c\u4fee\u6539\u56fe\u7247\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\uff0c\u53ea\u8981\u548c\u4fee\u6539js\u4ee3\u7801\u4e00\u6837\u5237\u65b0\u6a21\u62df\u5668\u6216\u771f\u673a\u5373\u53ef\u3002<\/li>\n<li>\u4e0e\u8bbf\u95ee\u7f51\u7edc\u56fe\u7247\u76f8\u6bd4\uff0cPackager\u53ef\u4ee5\u5f97\u77e5\u56fe\u7247\u5927\u5c0f\uff0c\u4e0d\u9700\u8981\u5728\u4ee3\u7801\u91cc\u518d\u58f0\u660e\u4e00\u904d\u5c3a\u5bf8\u3002<\/li>\n<li>\u53ef\u4ee5\u901a\u8fc7npm\u6765\u5206\u53d1\u7ec4\u4ef6\u6216\u5e93\u53ef\u4ee5\u5305\u542b\u56fe\u7247\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u4e3a\u4e86\u4f7f\u65b0\u7684\u56fe\u7247\u8d44\u6e90\u673a\u5236\u6b63\u5e38\u5de5\u4f5c\uff0crequire\u4e2d\u7684\u56fe\u7247\u540d\u5b57\u5fc5\u987b\u662f\u4e00\u4e2a\u9759\u6001\u5b57\u7b26\u4e32\u3002<\/p>\n<\/blockquote>\n<h2>\u4f7f\u7528\u6df7\u5408App\u7684\u56fe\u7247\u8d44\u6e90<\/h2>\n<p>\u5982\u679c\u4f60\u5728\u7f16\u5199\u4e00\u4e2a\u6df7\u5408App\uff08\u4e00\u90e8\u5206UI\u4f7f\u7528React Native\uff0c\u800c\u53e6\u4e00\u90e8\u5206\u4f7f\u7528\u5e73\u53f0\u539f\u751f\u4ee3\u7801\uff09\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5df2\u7ecf\u6253\u5305\u5230App\u4e2d\u7684\u56fe\u7247\u8d44\u6e90\uff08\u901a\u8fc7Xcode\u7684asset\u7c7b\u76ee\u6216\u8005Android\u7684drawable\u6587\u4ef6\u5939\u6253\u5305\uff09<\/p>\n<pre><code class=\"language-html\">&lt;Image source={{uri: &#039;app_icon&#039;}} style={{width: 40, height: 40}} \/&gt;<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u8fd9\u4e00\u505a\u6cd5\u5e76\u6ca1\u6709\u4efb\u4f55\u5b89\u5168\u68c0\u67e5\u3002\u4f60\u9700\u8981\u81ea\u5df1\u786e\u4fdd\u56fe\u7247\u5728\u5e94\u7528\u4e2d\u786e\u5b9e\u5b58\u5728\uff0c\u800c\u4e14\u8fd8\u9700\u8981\u6307\u5b9a\u5c3a\u5bf8\u3002<\/p>\n<\/blockquote>\n<p>\u6e90\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n *\/\n&#039;use strict&#039;;\n\nimport React, { Component } from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    PixelRatio,\n    Text,\n    Image,\n    TouchableOpacity,\n    View\n} from &#039;react-native&#039;;\n\nvar imgs = [\n    &#039;https:\/\/www.baidu.com\/img\/bd_logo1.png&#039;,\n    &#039;http:\/\/y0.ifengimg.com\/ifeng\/index\/20150921\/ifengLogo.png&#039;,\n    &#039;http:\/\/mat1.gtimg.com\/www\/images\/qq2012\/qqlogo_2x.png&#039;\n];\n\nclass RNAPP extends Component {\n    render() {\n        return (\n            &lt;View style={[styles.flex,{marginTop:45}]}&gt;\n                &lt;MyImage imgs={imgs}&gt; &lt;\/MyImage&gt;\n            &lt;\/View&gt;\n        );\n    }\n}\n\nclass MyImage extends Component {\n    constructor(props) {\n        super(props);\n        this.state = {\n            count: 0,\n            imgs: this.props.imgs,\n        };\n    }\n\n    \/\/source={{uri: this.state.imgs[this.state.count]}}  \/\/\u7f51\u7edc\u56fe\u7247\n    \/\/source={require(&#039;.\/rnapp.png&#039;)}  \/\/\u672c\u5730\u56fe\u7247\n\n    render() {\n        return(\n            &lt;View style={[styles.flex,{alignItems:&#039;center&#039;}]}&gt;\n                &lt;View style={styles.imageLayout}&gt;\n                    &lt;Image style={styles.image}\n                        resizeMode=&quot;contain&quot;\n                        source={{uri: this.state.imgs[this.state.count]}}\n                        \/&gt;\n                &lt;\/View&gt;\n\n                &lt;View style={styles.btnLayout}&gt;\n                    &lt;TouchableOpacity onPress={this.goPreview.bind(this)}&gt;\n                        &lt;View style={styles.btn}&gt;\n                            &lt;Text&gt;\u4e0a\u4e00\u5f20&lt;\/Text&gt;\n                        &lt;\/View&gt;\n                    &lt;\/TouchableOpacity&gt;\n                    &lt;TouchableOpacity onPress={this.goNext.bind(this)}&gt;\n                        &lt;View style={styles.btn}&gt;\n                            &lt;Text&gt;\u4e0b\u4e00\u5f20&lt;\/Text&gt;\n                        &lt;\/View&gt;\n                    &lt;\/TouchableOpacity&gt;\n                &lt;\/View&gt;\n            &lt;\/View&gt;\n        );\n    }\n\n    goPreview() {\n        var count=this.state.count;\n        count--;\n        if(count &gt;= 0){\n            this.setState({\n                count: count,\n            });\n        }\n    }\n\n    goNext() {\n        var count=this.state.count;\n        count++;\n        if(count &lt; this.state.imgs.length){\n            this.setState({\n                count: count,\n            });\n        }\n    }\n}\n\nconst styles = StyleSheet.create({\n    flex:{\n        flex:1,\n    },\n    btnLayout:{\n        flexDirection:&#039;row&#039;,\n        marginTop:20,\n        justifyContent:&#039;center&#039;,\n    },\n    btn:{\n        width:60,\n        height:30,\n        borderColor:&#039;#0089FF&#039;,\n        borderWidth:1,\n        justifyContent:&#039;center&#039;,\n        alignItems:&#039;center&#039;,\n        borderRadius:3,\n        marginRight:20,\n    },\n    imageLayout:{\n      borderWidth:1,\n        width:300,\n        height:200,\n        borderRadius:5,\n        borderColor:&#039;#ccc&#039;,\n        justifyContent:&#039;center&#039;,\n        alignItems:&#039;center&#039;,\n    },\n    image:{\n        height:150,\n        width:200,\n    }\n});\n\nAppRegistry.registerComponent(&#039;RNAPP&#039;, () =&gt; RNAPP);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>React Native\u7684Image\u7ec4\u4ef6\u8c03\u7528\u7684\u56fe\u7247\u9014\u5f84\u6bd4\u8f83\u591a\uff1a\u7f51\u7edc\u56fe\u7247\u3001\u672c\u5730\u56fe\u7247\u3001\u7167\u76f8\u673a\u56fe\u7247 Image\u7ec4\u4ef6\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[],"class_list":["post-489","post","type-post","status-publish","format-standard","hentry","category-react-native"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=489"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/489\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}