【问题标题】:Error loading image through localforage blob on ios通过 ios 上的 localforage blob 加载图像时出错
【发布时间】:2017-01-03 06:06:55
【问题描述】:

我正在通过 Cordova 相机 API 在 ios 设备上获取相机图像,然后通过 localforage 保存它。但是,似乎没有加载资源,因为单击Safari Web InspectorResources 标记下的blob 显示An error occured while trying to load resources 错误,并且图像呈现为20x20 白色背景而不是照片。这是我围绕Cordova Camera API 的HTML 包装器:

<head>
    <title>Capture Photo</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8" src="js/camera.js"></script>
    <script type="text/javascript" charset="utf-u" src="js/localforage.js"></script>

  <script type="text/javascript">
   function savePhoto(contents){
      localforage.setItem('photo', contents, function(img) {
            // This will be a valid blob URI for an <img> tag.
            var blob = new Blob([img], { type: "image/jpeg" } );
            var imageURI = window.URL.createObjectURL(blob);
            console.log(imageURI);
            var newImg = document.createElement("img");
                newImg.src=imageURI;
                // add the newly created element and its content into the DOM 
                var currentDiv = document.getElementById("div1"); 
                document.body.insertBefore(newImg, currentDiv); 
        });
      console.log(contents.byteLength);
        }

    function grabPhoto(){
      capturePhotoEdit(savePhoto,function(e){console.log(e);});
    }

  </script>

  </head>
  <body>
    <button onclick="grabPhoto();">Capture Photo</button> <br>
    <div id="div1">&nbsp;</div>
  </body>
</html>

我们使用savePhoto 来保存从相机抓取的图像(在js/camera.js 中)。 blob 的控制台日志会打印出 blob:null/916d1852-c8c5-4b3b-ac8c-86b6c71d0e86 之类的内容。

我将非常感谢关于我在图像渲染方面做错了什么的指针。

【问题讨论】:

    标签: javascript ios cordova local-storage localforage


    【解决方案1】:

    现在已通过切换到“Promise”样式 API 进行修复。不知何故,它不适用于异步回调 API 版本。固定JS代码如下:

    function savePhoto(contents){
    
          localforage.setItem('photo', contents).then(function(image) {
                // This will be a valid blob URI for an <img> tag.
                var blob = new Blob([image],{ type:"image/jpeg" });
                console.log('size=' + blob.size);
                console.log('type=' + blob.type);
                var imageURI = window.URL.createObjectURL(blob);
                var newImg = document.createElement("img");
                newImg.src=imageURI;
                newImg.onload = function() {
                   URL.revokeObjectURL(imageUrI);
                };
                var currentDiv = document.getElementById("div1"); 
                document.body.insertBefore(newImg, currentDiv); 
    
            }).catch(function(err) {
              // This code runs if there were any errors
              console.log(err);
            });
    }
    
    function grabPhoto(){
      capturePhotoEdit(savePhoto,function(e){console.log(e);});
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 2012-09-08
      • 2013-11-02
      相关资源
      最近更新 更多