選擇圖片時可設置圖片是否是原圖,圖片來源。這用的也挺常見的,比如個人中心中設置頭像,可以與wx.upLoadFile()API使用
主要方法:
wx.chooseImage(object)
wxml
<!--監聽按鈕--><button type="primary" bindtap="listenerButtonChooseImage">點擊我選擇相冊</button><!--通過數據綁定的方式動態獲取js數據--><image src="{{source}}" mode="aspecFill" style="width: 640rpx; height: 640rpx"/>
js
Page({ data:{ // text:"這是一個頁面" source: '' }, /** * 選擇相冊或者相機 配合上傳圖片接口用 */ listenerButtonChooseImage: function() { var that = this; wx.chooseImage({ count: 1, //original原圖,compressed壓縮圖 sizeType: ['original'], //album來源相冊 camera相機 sourceType: ['album', 'camera'], //成功時會回調 success: function(res) { //重繪視圖 that.setData({ source: res.tempFilePaths }) } }) },
wx.previewImage(object)
這又是一個奇葩API真實搞不懂怎么用這個。先模仿下官方咋使用但是沒有效果,搞懂了在補充下自己的使用
wxml
<!--圖片預覽--><button type="primary" bindtap="listenerButtonPreviewImage">展示圖片</button>
js
Page({ data:{ // text:"這是一個頁面" source: '' }, /** * 預覽圖片 又一個奇葩接口 */ listenerButtonPreviewImage: function() { wx.previewImage({ current: 'http://img.souutu.com/2016/0511/20160511055648316.jpg', urls: [ 'http://img.souutu.com/2016/0511/20160511055648316.jpg', 'http://img.souutu.com/2016/0511/20160511055650751.jpg', 'http://img.souutu.com/2016/0511/20160511054928658.jpg' ], //這根本就不走 success: function(res) { console.log(res); }, //也根本不走 fail: function() { console.log('fail') } }) }})
感謝閱讀本文,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答