微信小程序 (wx.chooseImage)

406 阅读1分钟
wx.chooseImage : 相机or从手机相册中选择图片

wx.chooseImage({
    count: 9, // 默认9
    sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
    // sourceType : 可以指定来源是相册还是相机,默认二者都有
    sourceType: ['album'],  // 此为相册 
    sourceType: ['camera'], // 此为相机
    success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;  // 只可以选择一次多张或一次一张

        // 若多次选择一张, 需把每次上传的图片拼接在一个数组中渲染;
        var tempFile = res.tempFilePaths;
        for (var i in res.tempFilePaths) {
        tempFilePaths = tempFilePaths.concat(res.tempFilePaths[i])
        }

        // 删除数组中指定的元素, 返回的是被删除的元素; 此时查看原数组中还存在2个元素(已删除元素不存在)
        var filePaths = tempFilePaths.splice(filePathsIndex, 1);

        //  so.. 渲染数据注意 应渲染原数组,而不是删除之后返回的新数组 (filePaths no | tempFilePaths yes)
        // 渲染图片
        that.setData({      
            tempFilePaths: tempFilePaths,
        })
    }
})