W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
解釋:從本地相冊(cè)選擇圖片或使用相機(jī)拍照。
Object object
屬性名 | 類型 | 必填 | 默認(rèn)值 | 說(shuō)明 |
---|---|---|---|---|
count | Number | 否 | 9 | 最多可以選擇的圖片張數(shù) |
sizeType | Array.<string> | 否 | original 原圖,compressed 壓縮圖,默認(rèn)二者都有 | |
sourceType | Array.<string> | 否 | album 從相冊(cè)選圖,camera 使用相機(jī),默認(rèn)二者都有 | |
success | Function | 否 | 成功則返回圖片的本地文件路徑列表 tempFilePaths | |
fail | Function | 否 | 接口調(diào)用失敗的回調(diào)函數(shù) | |
complete | Function | 否 | 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行) |
參數(shù) | 類型 | 說(shuō)明 | Web 態(tài)說(shuō)明 |
---|---|---|---|
tempFilePaths | Array.< string> | 圖片的本地文件路徑列表 | Web 態(tài)值為瀏覽器所支持的 blob URL 數(shù)組,形如 |
tempFiles | Array.<object> | 圖片的本地文件列表,每一項(xiàng)是一個(gè) File 對(duì)象 | - |
字段 | 類型 | 說(shuō)明 | Web 態(tài)說(shuō)明 |
---|---|---|---|
path | String | 本地文件路徑 | Web 態(tài)值為瀏覽器所支持的 blob URL 數(shù)組,示例 |
size | Number | 本地文件大?。▎挝唬築) | - |
代碼示例 1:tempFilePaths
<view class="wrap">
<view class="card-area">
<view s-if="{{imageList.length > 0}}" class="image-container" style="height: {{imageList.length > 6 ? '3.54' : '2.55'}}rem;">
<image s-for="image in imageList" class="image-items" style="height: {{imageList.length > 6 ? '32' : '49'}}%;" src="{{image}}" mode="scaleToFill" data-src="{{image}}" bindtap="previewImage"></image>
</view>
<view s-else class="display-area">
圖片顯示區(qū)
</view>
<view class="middle-area border-top">
<view class="list-area border-bottom" hover-class="option-active">
<text class="list-item-key-4">圖片來(lái)源</text>
<picker class="list-item-value" mode="selector" value="{{sourceIndex}}" range="{{sourceArray}}" bind:change="sourceChange">
<view hover-class="hover">{{sourceArray[sourceIndex]}}</view>
</picker>
</view>
<view class="list-area border-bottom" hover-class="option-active">
<text class="list-item-key-4">圖片質(zhì)量</text>
<picker class="list-item-value" mode="selector" value="{{sizeIndex}}" range="{{sizeArray}}" bind:change="sizeChange">
<view hover-class="hover">{{sizeArray[sizeIndex]}}</view>
</picker>
</view>
<view class="list-area border-bottom" hover-class="option-active">
<text class="list-item-key-4">圖片數(shù)量</text>
<picker class="list-item-value" mode="selector" value="{{countIndex}}" range="{{countArray}}" bind:change="countChange">
<view hover-class="hover">{{countArray[countIndex]}}</view>
</picker>
</view>
</view>
<view class="button-group">
<button type="primary" bindtap="selectImage">添加圖片</button>
<button type="default" bindtap="clearImage">清空?qǐng)D片</button>
</view>
</view>
</view>
Page({
data: {
sourceIndex: 2,
sourceArray: ['拍照', '相冊(cè)', '拍照或相冊(cè)'],
sizeIndex: 2,
sizeArray: ['壓縮', '原圖', '壓縮或原圖'],
countIndex: 0,
countArray: [],
imageList: []
},
onLoad(e) {
const array = [];
for (let i = 0; i < 9; i++) {
array.push(i + 1);
}
this.setData({
countIndex: array.length - 1,
countArray: array
});
},
sourceChange(e) {
this.setData('sourceIndex', e.detail.value);
},
sizeChange(e) {
this.setData('sizeIndex', e.detail.value);
},
countChange(e) {
this.setData('countIndex', e.detail.value);
},
selectImage() {
const sourceIndex = this.data.sourceIndex;
const count = this.data.countIndex + 1;
if (sourceIndex === 2) {
swan.showActionSheet({
itemList: ['拍照', '相冊(cè)'],
success: res => {
const sourceType = res.tapIndex === 0 ? 'camera' : 'album';
this.chooseImage(sourceType, count);
}
});
} else {
const sourceType = sourceIndex === 0 ? 'camera' : 'album';
this.chooseImage(sourceType, count);
}
},
chooseImage(sourceType, count) {
const sizeIndex = this.data.sizeIndex;
let sizeType = ['compressed', 'original'];
if (sizeIndex === 0) {
sizeType = ['compressed'];
} else if (sizeIndex === 1) {
sizeType = ['original'];
}
swan.chooseImage({
count,
sizeType,
sourceType: [sourceType],
success: res => {
this.setData('imageList', res.tempFilePaths);
console.log('res.tempFilePaths', res.tempFilePaths);
},
fail: err => {
console.log('chooseImage fail', err);
}
});
},
clearImage() {
const imageList = this.data.imageList;
if (imageList.length > 0) {
this.setData('imageList', []);
swan.showToast({title: '清空?qǐng)D片成功'});
} else {
swan.showToast({title: '無(wú)可清空?qǐng)D片', icon: 'none'});
}
},
previewImage(e) {
swan.showToast({title: '暫不支持預(yù)覽', icon: 'none'});
}
});
代碼示例 2:tempFiles
<view class="wrap">
<view class="card-area">
<view s-if="{{imageList.length > 0}}" class="image-container" style="height: {{imageList.length > 6 ? '3.54' : '2.55'}}rem;">
<image s-for="image in imageList" class="image-items" style="height: {{imageList.length > 6 ? '32' : '49'}}%;" src="{{image}}" mode="scaleToFill" data-src="{{image}}" bindtap="previewImage"></image>
</view>
<view s-else class="display-area">
圖片顯示區(qū)
</view>
<view class="middle-area border-top">
<view class="list-area border-bottom" hover-class="option-active">
<text class="list-item-key-4">圖片來(lái)源</text>
<picker class="list-item-value" mode="selector" value="{{sourceIndex}}" range="{{sourceArray}}" bind:change="sourceChange">
<view hover-class="hover">{{sourceArray[sourceIndex]}}</view>
</picker>
</view>
<view class="list-area border-bottom" hover-class="option-active">
<text class="list-item-key-4">圖片質(zhì)量</text>
<picker class="list-item-value" mode="selector" value="{{sizeIndex}}" range="{{sizeArray}}" bind:change="sizeChange">
<view hover-class="hover">{{sizeArray[sizeIndex]}}</view>
</picker>
</view>
<view class="list-area border-bottom" hover-class="option-active">
<text class="list-item-key-4">圖片數(shù)量</text>
<picker class="list-item-value" mode="selector" value="{{countIndex}}" range="{{countArray}}" bind:change="countChange">
<view hover-class="hover">{{countArray[countIndex]}}</view>
</picker>
</view>
</view>
<view class="button-group">
<button type="primary" bindtap="selectImage">添加圖片</button>
<button type="default" bindtap="clearImage">清空?qǐng)D片</button>
</view>
</view>
</view>
Page({
data: {
sourceIndex: 2,
sourceArray: ['拍照', '相冊(cè)', '拍照或相冊(cè)'],
sizeIndex: 2,
sizeArray: ['壓縮', '原圖', '壓縮或原圖'],
countIndex: 0,
countArray: [],
imageList: []
},
onLoad(e) {
const array = [];
for (let i = 0; i < 9; i++) {
array.push(i + 1);
}
this.setData({
countIndex: array.length - 1,
countArray: array
});
},
sourceChange(e) {
this.setData('sourceIndex', e.detail.value);
},
sizeChange(e) {
this.setData('sizeIndex', e.detail.value);
},
countChange(e) {
this.setData('countIndex', e.detail.value);
},
selectImage() {
const sourceIndex = this.data.sourceIndex;
const count = this.data.countIndex + 1;
if (sourceIndex === 2) {
swan.showActionSheet({
itemList: ['拍照', '相冊(cè)'],
success: res => {
const sourceType = res.tapIndex === 0 ? 'camera' : 'album';
this.chooseImage(sourceType, count);
}
});
} else {
const sourceType = sourceIndex === 0 ? 'camera' : 'album';
this.chooseImage(sourceType, count);
}
},
chooseImage(sourceType, count) {
const sizeIndex = this.data.sizeIndex;
let sizeType = ['compressed', 'original'];
if (sizeIndex === 0) {
sizeType = ['compressed'];
} else if (sizeIndex === 1) {
sizeType = ['original'];
}
swan.chooseImage({
count,
sizeType,
sourceType: [sourceType],
success: res => {
console.log('res.tempFiles',res.tempFiles);
let imageList = [];
res.tempFiles.forEach((e)=>{
imageList.push(e.path)
})
this.setData('imageList', imageList);
},
fail: err => {
console.log('chooseImage fail', err);
}
});
},
clearImage() {
const imageList = this.data.imageList;
if (imageList.length > 0) {
this.setData('imageList', []);
swan.showToast({title: '清空?qǐng)D片成功'});
} else {
swan.showToast({title: '無(wú)可清空?qǐng)D片', icon: 'none'});
}
},
previewImage(e) {
swan.showToast({title: '暫不支持預(yù)覽', icon: 'none'});
}
});
錯(cuò)誤碼 | 說(shuō)明 |
---|---|
202 | 解析失敗,請(qǐng)檢查參數(shù)是否正確 |
1002 | 用戶取消操作錯(cuò)誤碼 |
錯(cuò)誤碼 | 說(shuō)明 |
---|---|
202 | 解析失敗,請(qǐng)檢查參數(shù)是否正確 |
1002 | 用戶取消操作錯(cuò)誤碼 |
1003 | 用戶沒(méi)有授權(quán)百度使用相冊(cè) |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: