通过PhoneGap将PNG或JPG图像保存到iOS中的照片

时间:2012-10-25 01:35:12

标签: ios image cordova base64 photos

我是PhoneGap的新手,想了解如何将应用内图片保存到iOS中的照片。

虽然我能够使用

navigator.camera.getPicture

带选项

quality:50, destinationType:Camera.DestinationType.DATA_URL,saveToPhotoAlbum: true

将相机拍摄的照片保存到照片,我现在正在尝试了解如何将应用内图像保存到照片。

考虑以下PhoneGap - Cordova页面,其中元素myPhoto包含imagedata,例如“data:image / jpeg; base64”,...

<html>
    <head>
    <title>Save Image</title>
    <script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"></script>
    <script type="text/javascript" charset="utf-8">


    function savePicture(){
                    //this is where the magic would happen
                    //how can I save myPhoto to Photos?
                    //output imageData to a jpg or png file which would show up in Photos?
    }

        </script>
</head>
<body>
    <button onclick="savePicture();">Save Photo</button> <br>
            <input type="hidden" name="myPhoto" id="myPhoto" value="">
</body>
</html>

问题:

savePicture()应该通过Phonegap将图像数据从myPhoto保存到iOS中的照片?

2 个答案:

答案 0 :(得分:4)

也许你可以尝试我为IOS编写的插件(如果你想将canvas元素保存到photogallery,你可以使用下面的 downloadWithUrl 方法获得画布的dataURI)。希望它适合你。

这是git链接:https://github.com/Nomia/ImgDownloader

简短示例:

document.addEventListener("deviceready",onDeviceReady);

//google logo url
url = 'https://www.google.com/images/srpr/logo11w.png';

onDeviceReady = function(){
    cordova.plugins.imgDownloader.downloadWithUrl(url,function(){
        alert("success");
    },function(){
        alert("error");
    });        
}

//also you can try dataUri like: 1px gif
//url = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'

您还可以使用下载方法

将本地文件保存到图库

答案 1 :(得分:2)

我最近遇到了以下Phonegap插件,用于将Canvas图像保存到iOS中的照片: https://github.com/devgeeks/Canvas2ImagePlugin

按照自述文件instructions导入插件,然后您可以通过以下方式使用它:

<html>
    <head>
        <title>Save Image</title>
        <script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"></script>
        <script type="text/javascript" charset="utf-8" src="../Canvas2Image/Canvas2ImagePlugin.js"></script>
        <script type="text/javascript" charset="utf-8">

        var canvas2ImagePlugin; //devgeeks plugin for saving canvas images to photo gallery

        function onDeviceReady() {
             canvas2ImagePlugin = window.plugins.canvas2ImagePlugin;
         }

        function savePicture(){
            canvas2ImagePlugin.saveImageDataToLibrary(function(msg){console.log(msg);},function(err){console.log(err);},'mycanvas');
         }

        </script>
    </head>
    <body>
        <canvas id="myCanvas" width="500px" height="300px"> <strong>[Your browser can not show this example.]</strong> </canvas>
        <button onclick="savePicture();">Save Photo</button> <br>
    </body>
</html>