mikoshu
V2EX  ›  问与答

微信 jsapi 选择本地图片放入 canvas 安卓居然报跨域??

  •  
  •   mikoshu · Aug 23, 2019 · 2747 views
    This topic created in 2493 days ago, the information mentioned may be changed or developed.

    微信 js-sdk 接口 chooseImage 选择本地图片后生成的 localid 可以直接放入 img 标签里预览,这时候如果需要对图片裁切后生成 base64code IOS 和微信开发者工具正常 但是安卓居然提示 "Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported"

    试过 img.setAttribute("crossOrigin",'Anonymous') 无效

    估计现在只能将图片上传到微信服务器然后让后端去微信那边下载图片再返回一个允许跨域的图片 url 给我,来实现图片裁切了....

    6 replies    2019-08-23 14:31:47 +08:00
    TomVista
        1
    TomVista  
       Aug 23, 2019
    盲猜 file:url
    mikoshu
        2
    mikoshu  
    OP
       Aug 23, 2019
    @TomVista 错 是 wxLocalResource://imageidxxxxxxx
    mikoshu
        3
    mikoshu  
    OP
       Aug 23, 2019
    @TomVista 不过估计也是被微信反代为 file:url
    ESeanZ
        4
    ESeanZ  
       Aug 23, 2019
    FileReader 读取文件直接装 base64 canvas 导入 base64 的图片再做裁剪 裁剪后再转 base64 到得最终结果
    nihiue
        5
    nihiue  
       Aug 23, 2019 via Android
    楼上正解
    nihiue
        6
    nihiue  
       Aug 23, 2019 via Android
    还有个备选方案
    const img = new Image();
    img.setAttribute('crossOrigin','Anonymous')
    img.src = 'XXXXXXXX';
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2542 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 02:11 · PVG 10:11 · LAX 19:11 · JFK 22:11
    ♥ Do have faith in what you're doing.