如何使用 PhoneGap 将图像保存到 iPhone 照片库?
我正在为 iPhone 创建一个 PhoneGap 应用程序,用于显示图像库。有些图像包含在项目安装中,有些来自网络。当用户单击图像时,我希望他们能够将图像保存到 iPhone 照片库(照片)。最初,我希望该应用程序能够让用户将图像设置为壁纸,但发现这非常困难或不可能。
我只能找到一种使用 PhoneGap 和 Objective C 保存图像的解决方案,但 PhoneGap 解决方案包含已弃用的 PhoneGapCommand 类。我尝试使用 PGPlugin,但无法使代码正常工作。
我希望有人可能拥有可以执行此任务的当前 PhoneGap 解决方案,或者如果有人可以为我指出正确的方向,我将非常感激!
I am creating a PhoneGap app for the iPhone that displays a gallery of images. Some images are included with the project installation, and some are from the web. When a user clicks on an image, I would like them to be able to save the image to an iPhone photo gallery (Photos). Originally, I would have liked the app to let the user set the image as wallpaper, but found that it would be extremely difficult or impossible.
I could only find one solution for saving the image using PhoneGap and Objective C, but the PhoneGap solution contained the class PhoneGapCommand which is deprecated. I tried using PGPlugin, but could not get the code to work.
I am hoping that someone out there might have a current PhoneGap solution that can perform this task, or if anyone could point me in the right direction, I would really appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这个问题很旧,但我花了一天时间才弄清楚,因为这个示例不再适用于新版本的 Cordova。我目前使用的是 2.5.0 版本,所以我想我应该分享这个,这样其他人就不必经历我所经历的痛苦。
要保存图像,您必须编写自己的插件。步骤如下:
打开 Cordova XCODE 项目并编辑 config.xml。为您要创建的插件添加一个条目。
“name”是您将使用的 JavaScript 命名空间,“value”是 Objective-C 类名。
在您的 XCODE 项目中,找到“插件”组。右键单击它并从上下文菜单中选择“新建文件...”。添加新的类文件“MySaveImageToAlbum”。它应该继承自 CDVPlugin。
下面是头文件和实现文件的代码:
// MySaveImageToAlbum.h
#导入
// MySaveImageToAlbum.m
#import“CDVSaveImageToAlbum.h”
接下来,您需要创建一个调用此本机代码的 javascript 文件。代码如下:
var SaveImageToAlbum =
{
saveImageToAlbum:函数(成功回调,错误回调,参数)
{
cordova.exec(successCallback, errorCallback, "SaveImageToAlbum", "saveImageToAlbum", [args]);
}
};
在您的index.html 中引用第4 步中创建的JavaScript。假设您有一个画布,并且想要将其作为图像保存到相机胶卷中,则可以使用其“toDataURL()”函数返回一个base64 PNG数据。然后,您可以调用 saveImageToAlbum 函数并将其作为参数传递,如下所示:
SaveImageToAlbum.saveImageToAlbum(函数(e){
navigator.notification.alert("图片已成功保存到相册中。", null, "图片已保存!");
},
函数(e){
navigator.notification.alert("图片无法保存到相册中。", null, "保存错误!");
},
canvas.toDataURL());
就是这样!
希望您喜欢...
问候,
安东尼奥·C·洛加尔塔三世
I know this question is old but it took me a day to figure this out since this example is no longer applicable to the new version of Cordova. I am currently using version 2.5.0, so I thought I'd share this so others would not have to go through the pain I did.
To save an image, you'll have to write your own plug-in. Here are the steps:
Open your Cordova XCODE project and edit config.xml. Add an entry for the plugin you'll be creating.
"name" is the JavaScript namespace you'll be using and "value" is the Objective-C class name.
In your XCODE project, locate the "Plugins" group. Right click it and select "New File..." from the context menu. Add a new class file "MySaveImageToAlbum". It should be inherited from CDVPlugin.
Below are the codes for the header and the implementation files:
// MySaveImageToAlbum.h
#import
// MySaveImageToAlbum.m
#import "CDVSaveImageToAlbum.h"
Next, you'll need to create a javascript file that calls this native code. Here's the code:
var SaveImageToAlbum =
{
saveImageToAlbum: function(successCallback,errorCallback,args)
{
cordova.exec(successCallback, errorCallback, "SaveImageToAlbum", "saveImageToAlbum", [args]);
}
};
Make a reference to the JavaScript created in no.4 in your index.html. Suppose you have a canvas and you want to save it as an image to the camera roll, you can use its "toDataURL()" function to return a base64 PNG data. You can then call the saveImageToAlbum function and pass this as a parameter like this:
SaveImageToAlbum.saveImageToAlbum(function(e){
navigator.notification.alert("Image has been successfully saved in the Photo Album.", null, "Image saved!");
},
function(e){
navigator.notification.alert("Image could not be saved in the Photo Album.", null, "Save error!");
},
canvas.toDataURL());
That's it!
Hope you enjoy...
Regards,
Antonio C. Logarta III
我自己想出来了。我能够使用 Jesse MacFadyen 的 ImageHelper。
http://groups.google.com/group/phonegap/browse_thread/thread/ea7ee31887b2f610/fe2c0b127cf51e7a
http://groups.google.com/group/phonegap/tree/browse_frm/thread/8aeefbb9421f1b81/94963d9742b0738f?hide_quotes=no
当我第一次实现这个时,我的所有代码都是正确的,但我没有将 ImageHelper 添加为 PhoneGap.plist 插件部分中的条目。我还将已弃用的 PhoneGapCommand 更改为 PGPlugin。
I figured it out on my own. I was able to use Jesse MacFadyen's ImageHelper.
http://groups.google.com/group/phonegap/browse_thread/thread/ea7ee31887b2f610/fe2c0b127cf51e7a
http://groups.google.com/group/phonegap/tree/browse_frm/thread/8aeefbb9421f1b81/94963d9742b0738f?hide_quotes=no
When I first implemented this, I had all the code right, but I didn't have ImageHelper added as an entry in the PhoneGap.plist plugins section. I also changed the deprecated PhoneGapCommand to PGPlugin.