wx.getLocalImgData 微信调取摄像头拍照后获取图片api的兼容问题
作者:互联网
wx.chooseImage({
count: 1, //最多可以选择的图片张数
sizeType: ["original", "compressed"], //所选的图片的尺寸
sourceType: ["camera"], //选择图片的来源 'album', 'camera'
success: (res: any) => {
Toast.loading({
duration: 0,
message: "上传...",
forbidClick: true
});
localIds = res.localIds; //是数组
localIds.forEach((item, key) => {
wx.getLocalImgData({
localId: item, // 图片的localID
success: imageData => {
if (imageData.localData.indexOf(";base64,") == -1) {
//兼容处理,安卓获取的图片base64码没有前缀,而苹果有,base64前缀并不固定
imageData.localData =
"data:image/jpeg;base64," + imageData.localData; //加上base64前缀
}
if (imageData.localData.indexOf("data:image/jpg;base64,") != -1) {
//兼容处理,若是苹果手机,将前缀中的jgp替换成jpeg
imageData.localData = imageData.localData.replace(
"data:image/jpg;base64,",
"data:image/jpeg;base64,"
);
}
}
})
})
})
标签:前缀,imageData,兼容问题,微信,image,base64,localData,data,getLocalImgData 来源: https://www.cnblogs.com/chengxian-wang/p/14990548.html