base64 small micro-channel program into the local picture

Needs to be generated when generating the poster in the poster in the corresponding id commodity two-dimensional code, you can use a string into a two-dimensional code picture, but the picture format base64, micro-channel can be viewed in the normal developer tools, but the phone test can not be displayed, need base64 pictures into the local picture.

1. Create a new file base64src.js

// applet share of two-dimensional code is base64 format, in the production sharing chart, picture format needs to be converted to
`` `const FSM = wx.getFileSystemManager ();
const FILE_BASE_NAME = 'tmp_base64src'; // custom file name

function base64src(base64data, cb) {
const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
if (!format) {
return (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() {
cb(filePath);
},
fail() {
return (new Error('ERROR_BASE64SRC_WRITE'));
},
});
};

export { base64src };

#####2、在需要使用的文件中引入并使用

{} from base64src Import '../../utils/base64src.js'
Page ({
Data: {
shareQrImg: " Data: Image / JPEG; Base64, / 9j / 4AAQSkZJRgA ........ GASDFKGKF =" // base64 image
},
the onLoad: function (Options) {
base64src (this.data.shareQrImg, RES => {
the console.log (RES) // returns the address of the picture, the label can be directly assigned to the image
});
}
} )

#####3、如果需要网络图片转换成base64格式。

wx.request ({
URL: " https://s0.2mdn.net/simgad/10657937226496242109 ",
Method: 'the GET',
responseType: 'ArrayBuffer',
Success: (RES) => {
the let wx.arrayBufferToBase64 Base64 = ( res.data);
the let userImageBase64 = ' Data: image / JPG; base64 ,' + base64;
the console.log (userImageBase64); // print format picture base64
// if desired image using local cache, refer to Step
}
} )
`` `

Guess you like

Origin www.cnblogs.com/jessie-xian/p/11571609.html