本文編輯: 大妖怪瀏覽 8674
版權所有,嚴禁轉載
接口 | 說明 |
---|---|
wx.chooseImage | 從本地相冊選擇圖片或使用相機拍照 |
wx.previewImage | 預覽圖片 |
wx.getImageInfo | 獲取圖片信息 |
wxml
index.wxml
<import src="../template/imgtpl"/>
<button bindtap="chooseImg">上傳圖片</button>
<block wx:for="{{tempFilePaths}}">
<view>
<template is="imgItem" data="{{item}}"/>
<button bindtap="getImageInfo" id="{{item}}">獲取圖片信息</button>
<text>{{info}}</text>
</view>
</block>
imgtpl.wxml
<template name="imgItem">
<image src="{{item}}"></image>
</template>
js
chooseImg: function(){
var that = this;
wx.chooseImage({
count: 9, // 默認9
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
that.setData({
tempFilePaths:res.tempFilePaths
})
}
})
},
getImageInfo:function(e){
var imgUrl = e.currentTarget.id;
var that = this;
wx.getImageInfo({
src: imgUrl,
success: function (res) {
that.setData({
info:"圖片長度:"+res.height+"
圖片寬度:"+res.width
})
}
})
}
wxml
<image bindtap="previewImage" id="http://www.mkhyf.com/upload/column/c413eaf4-bfad-43bd-820b-538d5c7dcfd0.png" src="http://www.mkhyf.com/upload/column/c413eaf4-bfad-43bd-820b-538d5c7dcfd0.png"></image>
js
previewImage:function(e){
var imgUrl = e.currentTarget.id;
wx.previewImage({
current: imgUrl, // 當前顯示圖片的http鏈接
urls: [imgUrl] // 需要預覽的圖片http鏈接列表
})
}
【wx.chooseImage】:【從本地相冊選擇圖片或使用相機拍照。】
參數 | 類型 | 必填 | 描述 |
---|---|---|---|
count | Number | 否 | 最多可以選擇的圖片張數,默認9 |
sizeType | StringArray | 否 | original 原圖,compressed 壓縮圖,默認二者都有 |
sourceType | StringArray | 否 | album 從相冊選圖,camera 使用相機,默認二者都有 |
success | Function | 是 | 成功則返回圖片的本地文件路徑列表 tempFilePaths |
fail | Function | 否 | 接口調用失敗的回調函數 |
complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
wx.chooseImage({
count: 1, // 默認9
sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有
sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有
success: function (res) {
// 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片
var tempFilePaths = res.tempFilePaths
}
})
【wx.previewImage】:【預覽圖片】
參數 | 類型 | 必填 | 描述 |
---|---|---|---|
current | String | 否 | 當前顯示圖片的鏈接,不填則默認為 urls 的第一張 |
urls | StringArray | 是 | 需要預覽的圖片鏈接列表 |
sourceType | StringArray | 否 | album 從相冊選圖,camera 使用相機,默認二者都有 |
success | Function | 是 | 成功則返回圖片的本地文件路徑列表 tempFilePaths |
fail | Function | 否 | 接口調用失敗的回調函數 |
complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
wx.previewImage({
current: '', // 當前顯示圖片的http鏈接
urls: [] // 需要預覽的圖片http鏈接列表
})
【wx.getImageInfo】:【獲取圖片信息】
參數 | 類型 | 必填 | 描述 |
---|---|---|---|
src | String | 是 | 圖片的路徑,可以是相對路徑,臨時文件路徑,存儲文件路徑,網絡圖片路徑 |
success | Function | 是 | 成功則返回圖片的本地文件路徑列表 tempFilePaths |
fail | Function | 否 | 接口調用失敗的回調函數 |
complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
success返回參數說明:
參數 | 類型 | 必填 |
---|---|---|
width | Number | 圖片寬度,單位px |
height | Number | 圖片高度 單位px |
path | String | 返回圖片的本地路徑 |
wx.getImageInfo({
src: 'images/a.jpg',
success: function (res) {
console.log(res.width)
console.log(res.height)
}
})
wx.chooseImage({
success: function (res) {
wx.getImageInfo({
src: res.tempFilePaths[0],
success: function (res) {
console.log(res.width)
console.log(res.height)
}
})
}
})