场景
自己在参考小程序官方文档,使用camera组件时,无法拍照。代码都是参考官方文档的,但无法正常拍照。
element.wxml文件:
<camera mode="normal" device-position="back" binderror="cameraBindError"
style="width: 100%; height: 300px;"
></camera>
<button type='primary' bindtap="takePhoto">拍照</button>
<view>预览</view>
<image mode="widthFix" style="width:100%; height:500px" src="{{camera_src}}"></image>
element.js文件:
// 拍照
takePhoto(event){
const cameraCtx = wx.createCameraContext(this);
cameraCtx.takePhoto({
quality:"high",
success:(res)=>{
this.setData({
camera_src: res.tempImagePath,
});
},
fail:(msg)=>{
wx.showToast({
title: "Error,相机拍照出错!!",
})
}
});
},
cameraBindError(event){
wx.showToast({
title: "Error,相机未授权!",
})
},
解决方案
和官方文档仔仔细细对比后,发现:在element.js文件中,我是这么写的,多了一个this参数。
const cameraCtx = wx.createCameraContext(this);
官方文档中:
const cameraCtx = wx.createCameraContext();
把自己的代码去掉this后,就可以正常拍照了。
网友评论