|
1.index.wxml
-
1 <!--index.wxml-->
-
2 <button style="margin:30rpx;" bindtap="chooseimage">獲取圖片</button>
-
3 <image src="{{tempFilePaths }}" mode="aspecFill" style="width: 100%; height: 450rpx"/>
2.index.js
-
1 //index.js
-
2 //獲取應用實例
-
3 var app = getApp()
-
4 Page({
-
5 data: {
-
6 tempFilePaths: ''
-
7 },
-
8 onLoad: function () {
-
9 },
-
10 chooseimage: function () {
-
11 var _this = this;
-
12 wx.chooseImage({
-
13 count: 1, // 默認9
-
14 sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有
-
15 sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有
-
16 success: function (res) {
-
17 // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片
-
18 _this.setData({
-
19 tempFilePaths:res.tempFilePaths
-
20 })
-
21 }
-
22 })
-
23 }
-
24 })
這里說說sourcetype.默(mo)認是(shi)從相冊(ce)獲取和使用相機拍照(zhao),跟微信現在(zai)選(xuan)擇圖片(pian)的界面一(yi)(yi)樣,第(di)一(yi)(yi)格是(shi)拍照(zhao),后面的是(shi)相冊(ce)照(zhao)片(pian).
這里注意:返(fan)回(hui)的(de)是圖(tu)片(pian)在本地的(de)路徑.如果需(xu)(xu)要(yao)(yao)將圖(tu)片(pian)上傳到(dao)(dao)服務器,需(xu)(xu)要(yao)(yao)用到(dao)(dao)另(ling)一個API.
示例代(dai)碼:
-
1 wx.chooseImage({
-
2 success: function(res) {
-
3 var tempFilePaths = res.tempFilePaths
-
4 wx.uploadFile({
-
5 url: '//example.weixin.qq.com/upload', //僅為示例,非真實的接口地址
-
6 filePath: tempFilePaths[0],
-
7 name: 'file',
-
8 formData:{
-
9 'user': 'test'
-
10 },
-
11 success: function(res){
-
12 var data = res.data
-
13 //do something
-
14 }
-
15 })
-
16 }
-
17 })
|