将base64转换为文件

var dataURLtoFile = function(dataurl, filename) {// 将base64转换为文件
      var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n)
      while(n--){
        u8arr[n] = bstr.charCodeAt(n)
      }
      return new File([u8arr], filename, {type: mime})
    }
    var capture = function(start_x, start_y, width, height){
      var renderText = new cc.RenderTexture(size.width, size.height)
      renderText.begin()
      // self.bg.visit()
      self.visit()
      renderText.end()

      var _screen = cc.Sprite.create(renderText.getSprite().getTexture(), cc.rect(start_x, size.height - start_y - height, width, height))

      var canvas = cc.newElement('canvas') // 创建一个新的元素节点
      canvas.width = size.width
      canvas.height = size.height
      var ctx = canvas.getContext('2d')
      ctx.drawImage(renderText.getSprite().getTexture().getHtmlElementObj(), 0, 0)
      var href = canvas.toDataURL('image/jpeg')
      var file = dataURLtoFile(href, 'scree.png')
      App.upImg(file)
      return _screen
    }

你可能感兴趣的:(将base64转换为文件)