Resolve the APICloud PNG photo base64 encoding failure

  • Problem description

When using trans module decodeImgToBase64 function, I found that the converted code cannot be displayed correctly when uploading. When I put the transformed code in the online Base64 to turn photos, there was a coding error. When I compared other base64 codes, I found that the Trans module decodeImgToBase64 was less

data:image/png; base64,Copy the code

This is the beginning.

  • The solution

Add “data:image/ PNG; Base64, “string.

Base64Data = "data:image/png; base64," + ret.base64Str;Copy the code
  • The complete code
function fnOpenDraw() {
        drawingBoard.open({
            rect: {
                x: 10.y: 120.w: api.winWidth - 20.h: 360
            },
            styles: {
                brush: {
                    color: '#00f'.width: 6
                },
                bgColor: '#fafff0'
            },
            fixedOn: api.frameName
        });
    }

    function fnSaveDraw() {
        drawingBoard.save({
            savePath: 'fs://drawingBoard/result.png'.copyToAlbum: false.overlay: true
        }, function(ret) {
            console.log(JSON.stringify(ret));
            convert2Base(ret.absolutePath);
        });
    }

    function fnClearDraw() {
        drawingBoard.clear();
    }

    function convert2Base(photoPath) {
        var trans = api.require('trans');
        trans.decodeImgToBase64({
            imgPath: photoPath
        }, function(ret, err) {
            if (ret.status) {
                console.log((ret.base64Str));
                //alert(ret.base64Str);
                Base64Data = "data:image/png; base64," + ret.base64Str;
                Base64DataOK = true;
            } else {
                console.log(JSON.stringify(ret)); }}); }Copy the code