天天看點

微信小程式chooseImage(從本地相冊選擇圖檔或使用相機拍照)

一、使用API wx.chooseImage(OBJECT)

var util = require(\'../../utils/util.js\')
Page({
    data:{
        src:"../image/pic4.jpg"
    },
    gotoShow: function(){var _this = this
        wx.chooseImage({
          count: 9, // 最多可以選擇的圖檔張數,預設9
          sizeType: [\'original\', \'compressed\'], // original 原圖,compressed 壓縮圖,預設二者都有
          sourceType: [\'album\', \'camera\'], // album 從相冊選圖,camera 使用相機,預設二者都有
          success: function(res){
            // success
            console.log(res)
            _this.setData({
                src:res.tempFilePaths
            })
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
    }      

二、圖檔路徑進行資料綁定

<view class="container">
  <view>
    <button type="default" bindtap="gotoShow" >點選上傳照片</button> 
  </view>
  <view>
    <image class= "show-image" mode="aspectFitf" src="{{src}}"></image>
  </view>
</view>      
号外:
  1、wx.chooseImage 調用相機或相冊      
  2、<image class= "show-image" mode="aspectFitf" src="{{src}}"></image> 資料綁定      
  3、js中動态修改檔案路徑
      
var _this = this
        wx.chooseImage({
          count: 9, // 最多可以選擇的圖檔張數,預設9
          sizeType: [\'original\', \'compressed\'], // original 原圖,compressed 壓縮圖,預設二者都有
          sourceType: [\'album\', \'camera\'], // album 從相冊選圖,camera 使用相機,預設二者都有
          success: function(res){
            // success
            console.log(res)
            _this.setData({
                src:res.tempFilePaths
            })
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }