uni-app 图片转base64
<div class="cnblogs_code"><pre><span style="color: rgba(0, 0, 0, 1)">urlBase64(url) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> toBase64Url;
uni.request({
url: url,
method: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">GET</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
responseType: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">arraybuffer</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
success: </span><span style="color: rgba(0, 0, 255, 1)">async</span> res =><span style="color: rgba(0, 0, 0, 1)"> {
let base64 </span>= wx.arrayBufferToBase64(res.data); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把arraybuffer转成base64</span>
toBase64Url = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">data:image/jpeg;base64,</span><span style="color: rgba(128, 0, 0, 1)">'</span> + base64; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">不加上这串字符,在页面无法显示</span>
<span style="color: rgba(0, 0, 0, 1)">console.log(toBase64Url);
}
});
}
let coverImg:</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://xxxx/xxx.jpg</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 0, 255, 1)">this</span>.urlTobase64(coverImg);</pre>
</div>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<div id="AllanboltSignature">
<div><span style="font-weight:700;color:#3a416f;">本文作者:</span>___mouM</div>
<div><span style="font-weight:700;color:#3a416f;">本文出处:</span>https://www.cnblogs.com/aknife/</div>
<div><span style="font-weight:700;color:#3a416f;">声援博主:</span>如果您觉得文章对您有帮助,可以点击文章右下角<span style="color:#5c8ec6">【推荐】</span>一下。</div>
<div><span style="font-weight:700;color:#3a416f;">版权说明:</span>本文版权归作者和博客园共有,欢迎转载。但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利.</div>
</div><br><br>
来源:https://www.cnblogs.com/aknife/p/12957767.html
頁:
[1]