preface
Wechat applet provides wx.getImageInfo(Object Object), but can only get pictures (network path, local path, code package path), not base64 format picture data.
The solution
Is a binary image file that converts base64 images to the local user path
- Base64 data is first converted to ArrayBuffer data using wx.Base64ToArrayBuffer
- Using FileSystemManager. WriteFile ArrayBuffer binary image file path for local users
- In this case, the image file path is in wx.env.USER_DATA_PATH, and the wx.getimageInfo interface can correctly obtain the image resource
Here is the base64src.js function code. Note that the base64 header is removed when the file is written:
const fsm = wx.getFileSystemManager();
const FILE_BASE_NAME = 'tmp_base64src';
const base64src = function(base64data) {
return new Promise((resolve, reject) => {
const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
if (!format) {
reject(new Error('ERROR_BASE64SRC_PARSE'));
}
const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`;
const buffer = wx.base64ToArrayBuffer(bodyData);
fsm.writeFile({
filePath,
data: buffer,
encoding: 'binary',
success() {
resolve(filePath);
},
fail() {
reject(new Error('ERROR_BASE64SRC_WRITE'));
},
});
});
};
export default base64src;
Copy the code