简体   繁体   English

将照片保存到Cordova中的iOs相册

[英]Save photo to iOs album in Cordova

Is there a way to download and save photo from URL to album/cameraroll in iOs using Cordova? 有没有办法使用Cordova在iO中将URL中的照片下载并保存到相册/相机胶卷中?

I used FileTransfer to download photos, but they don't show up like in android gallery. 我使用FileTransfer下载照片,但是它们不会像在android gallery中那样显示。 I guess there should be some plugin for that, but i cant find one. 我想应该有一些插件,但是我找不到。 Or some other method perhaps? 还是其他方法?

If it can't be done in Cordova, can be it done at all, so I could create plugin for Cordova in Objective C? 如果它不能在Cordova中完成,那可以做到吗,所以我可以在Objective C中为Cordova创建插件?

You will need to install Canvas2Image with your CLI like so: 您将需要使用CLI安装Canvas2Image,如下所示:

cordova plugin add https://github.com/devgeeks/Canvas2ImagePlugin.git

(or replace 'cordova' with 'phonegap' if you use that instead.) (如果您改用“ cordova”,则将其替换为“ phonegap”。)

Next, you will need to add a function (in this case saveImageToPhone()) that calls the plugin you just added to your project. 接下来,您将需要添加一个函数(在本例中为saveImageToPhone()),该函数调用刚添加到项目中的插件。 This function will be called from your button selector. 该功能将从您的按钮选择器中调用。 For instance: 例如:

<button onclick="saveMyPic("path/to/my/pic.jpg")">Save a pic to library</button>

It doesn't have to be a button, obviously, you can just make the function call and pass it any URL you like. 不必一定是按钮,显然,您可以调用函数并将其传递给您喜欢的任何URL。 This 'URL' can be the path that you get back from the FileTransfer success. 此“ URL”可以是您从FileTransfer成功获得的路径。

Here's my code: 这是我的代码:

function saveMyPic(myURL){
        var MEsuccess = function(msg){
           console.info(msg);
        };

        var MEerror = function(err){
            console.error(err);
        };

        saveImageToPhone(myURL, MEsuccess, MEerror);
}


function saveImageToPhone(url, success, error) {
    var canvas, context, imageDataUrl, imageData;
    var img = new Image();
    img.onload = function() {
        canvas = document.createElement('canvas');
        canvas.width = img.width;
        canvas.height = img.height;
        context = canvas.getContext('2d');
        context.drawImage(img, 0, 0);
        try {
            imageDataUrl = canvas.toDataURL('image/jpeg', 1.0);
            imageData = imageDataUrl.replace(/data:image\/jpeg;base64,/, '');
            cordova.exec(
                success,
                error,
                'Canvas2ImagePlugin',
                'saveImageDataToLibrary',
                [imageData]
        );
    }
    catch(e) {
        error(e.message);
    }
};
try {
    img.src = url;
}
catch(e) {
    error(e.message);
}

} Now just call the first function from wherever you wish. 现在,您可以从任意位置调用第一个函数。 If it works, you'll get a console.log that says 如果可行,您将获得一个console.log,内容为

IMAGE SAVED! 图像已保存!

I hope that helps you out! 希望对您有所帮助!

@WillCo solution probably would do the job, but creating canvas and converting canvas date do base64 to achive goal as simple as of downloading a photo seems unnecessary at least. @WillCo解决方案可能会完成这项工作,但是创建画布和将画布日期转换为实现base64的目标就像下载照片一样简单,至少似乎没有必要。

I recently dug up my old code of this project. 我最近挖了这个项目的旧代码。 I created a little native plugin for iOS that could download the image directly from the given URL to the system gallery on the device. 我为iOS创建了一个本地小插件,可以将图像直接从给定URL下载到设备上的系统库。

I posted my solution to GitHub: https://github.com/Kocik/cordova-photo-to-album-plugin 我将解决方案发布到了GitHub: https : //github.com/Kocik/cordova-photo-to-album-plugin

How it works: 这个怎么运作:

Install plugin: cordova plugin add https://github.com/Kocik/cordova-photo-to-album-plugin/ 安装插件: cordova plugin add https://github.com/Kocik/cordova-photo-to-album-plugin/

In your javascript add: window.plugins.phototoalbum.download(url, successCallback, failCallback); 在您的javascript中添加: window.plugins.phototoalbum.download(url, successCallback, failCallback);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM