swan.chooseImage
解释:从本地相册选择图片或使用相机拍照。
方法参数
Object object
object参数说明 :
success返回参数说明 :
tempFiles 对象结构如下:
示例
扫码体验
请使用百度APP扫码
图片示例
代码示例1 - tempFilePaths :
在开发者工具中预览效果 -
Page({ chooseImage() { swan.chooseImage({ count: 1, sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: res => { // 成功则返回图片的本地文件路径列表 tempFilePaths console.log(res.tempFilePaths); this.setData('imageList', res.tempFilePaths); }, fail: err => { console.log('错误码:' + err.errCode); console.log('错误信息:' + err.errMsg); } }); }});
代码示例2 - tempFiles :
在开发者工具中预览效果 -
Page({ chooseImage() { swan.chooseImage({ count: 1, sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: res => { // 文件列表对象 console.log(res.tempFiles); let imageList = []; res.tempFiles.forEach((e)=>{ imageList.push(e.path) }) this.setData('imageList', imageList); }, fail: err => { console.log('错误码:' + err.errCode); console.log('错误信息:' + err.errMsg); } }); }});
错误码
Android
iOS
Bug & Tip
- Web 态内,文件的临时路径仅在浏览器关闭前有效。受浏览器限制,在 Web 态无法使用 swan.saveFile 在本地持久保存文件。
