从cordova html页面(cordova)加载保存在iOS App Documents目录中的图片? [英] Load images saved in iOS App Documents directory from cordova html page (cordova)?

查看:1246
本文介绍了从cordova html页面(cordova)加载保存在iOS App Documents目录中的图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个应用程序,必须运行在iOS,Android和Windows Phone。所有工作精细除了iOS。我的应用程序使用相机拍摄照片,调整图像大小并保存图像。



在iOS上,这些图像被保存到应用程序的tmp目录中,正在删除,所以现在我将图像保存到Documents文件夹。



然后将此图像的路径保存到我的sqlite数据库。在我的html页面上,我使用url引用该文件,例如



var / mobile / application / GUID-HERE / Documents / imageName.jpg



现在看来,当我在xCode中重建我的应用程序时,应用程序guid被更改,所以以前保存的所有文件引用现在都无效了。



因此


  1. 我可以从我的HTML页面相对引用文档文件夹吗?


  1. 使用 toInternalURL() c $ c>文件插件的属性。保存图像后,您可以获取没有该GUID的本地网址,并将其保存到数据库。网址格式为

      cdvfile:// localhost / persistent / path / to / file 

    文档



    例如:
    (通常来自 file-transfer docs 。需要文件和文件传输插件)

      resolveLocalFileSystemURL(cordova.file.documentsDirectory,function success(entry){
    download(entry.toInternalURL()+image.jpg) ;
    });

    var download = function(localUrl){

    var fileTransfer = new FileTransfer();
    var uri = encodeURI(https://cordova.apache.org/images/cordova_bot.png);

    fileTransfer.download(
    uri,
    localUrl,
    function(entry){
    document.body.innerHTML = entry.toInternalURL();
    var img = document.createElement(img);
    img.setAttribute('src',entry.toInternalURL());
    document.body.appendChild(img);

    },
    function(error){
    console.log(error code+ error.code);
    },
    false,
    {
    headers:{
    Authorization:Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA ==
    }
    }
    );
    }


    I have built 1 app that has to run on iOS, android and Windows Phone. all work fine apart from iOS. My app takes a photo using the camera, resizes that image and saves the image.

    On iOS, these images were being saved into the tmp directory of the application, which was being deleted, so now i save the images into the Documents folder.

    I then save the path to this image into my sqlite database. On my html page, i reference the file using the url, such as

    var/mobile/application/GUID-HERE/Documents/imageName.jpg

    Now it seems that when i rebuild my application in xCode, the application guid is changed, so all my file references that have been previously saved, are now invalid.

    So

    1. Can i reference the documents folder relatively from my HTML page?

    OR

    1. Can i stop my application changing this GUID?

    解决方案

    Use the toInternalURL() property of the File plugin. After you save your image, you can get a local url without that GUID and save that to your database instead. The url has this format

    cdvfile://localhost/persistent/path/to/file
    

    Documentation

    An example: (mostly from file-transfer docs. requires file and file-transfer plugins)

    resolveLocalFileSystemURL(cordova.file.documentsDirectory, function success(entry) {
        download(entry.toInternalURL() + "image.jpg");
    });
    
    var download = function(localUrl) {
    
        var fileTransfer = new FileTransfer();
        var uri = encodeURI("https://cordova.apache.org/images/cordova_bot.png");
    
        fileTransfer.download(
          uri,
          localUrl,
          function(entry) {
              document.body.innerHTML = entry.toInternalURL();
              var img = document.createElement("img");
              img.setAttribute('src', entry.toInternalURL());
              document.body.appendChild(img);
    
          },
          function(error) {
              console.log("error code" + error.code);
          },
          false,
          {
              headers: {
                  "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
              }
          }
      );
    }
    

    这篇关于从cordova html页面(cordova)加载保存在iOS App Documents目录中的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆