如何使用 PhoneGap 将图像保存到 iPhone 照片库?

发布于 2024-12-23 02:01:08 字数 328 浏览 0 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情换悲伤 2024-12-30 02:01:08

我知道这个问题很旧,但我花了一天时间才弄清楚,因为这个示例不再适用于新版本的 Cordova。我目前使用的是 2.5.0 版本,所以我想我应该分享这个,这样其他人就不必经历我所经历的痛苦。

要保存图像,您必须编写自己的插件。步骤如下:

  1. 打开 Cordova XCODE 项目并编辑 config.xml。为您要创建的插件添加一个条目。

“name”是您将使用的 JavaScript 命名空间,“value”是 Objective-C 类名。

  1. 在您的 XCODE 项目中,找到“插件”组。右键单击它并从上下文菜单中选择“新建文件...”。添加新的类文件“MySaveImageToAlbum”。它应该继承自 CDVPlugin。

  2. 下面是头文件和实现文件的代码:

    // MySaveImageToAlbum.h
    #导入

    @interface MySaveImageToAlbum : CDVPlugin
     - (void)saveImageToAlbum:(CDVinvokedUrlCommand*)命令;
    @结尾
    

    // MySaveImageToAlbum.m
    #import“CDVSaveImageToAlbum.h”

    #import ;
    
    
    @implementationMySaveImageToAlbum
    
    - (void)saveImageToAlbum:(CDVinvokedUrlCommand*)命令
    {
        CDVPluginResult* 插件结果 = nil;
        NSURL *url = [NSURL URLWithString:[command.arguments objectAtIndex:0]];
        NSData *args = [NSData dataWithContentsOfURL:url];
    
    
        if (args != nil && [args 长度] > 0) {
    
            @尝试
            {
                UIImage *image = [UIImage imageWithData:args];
                NSData *imgdata = UIImagePNGRepresentation(图像);
                UIImage *image2 = [UIImage imageWithData:imgdata];
                UIImageWriteToSavedPhotosAlbum(image2, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
    
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"成功"];
                [self.commandDelegate sendPluginResult:pluginResult回调Id:command.callbackId];
            }
            @catch (NSException *异常) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                [self.commandDelegate sendPluginResult:pluginResult回调Id:command.callbackId];
            }
    
        } 别的 {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
            [self.commandDelegate sendPluginResult:pluginResult回调Id:command.callbackId];
        }
    
    }
    
    
    - (void)图像:(UIImage *)图像 didFinishSavingWithError:(NSError *)错误
      上下文信息:(void *)上下文信息
    {
        // 有错误吗?
        如果(错误!= NULL)
        {
            // 显示错误信息...
    
        }
        否则//没有错误
        {
            // 显示消息图片保存成功
    
        }
    }
    
    
    @结尾
    
  3. 接下来,您需要创建一个调用此本机代码的 javascript 文件。代码如下:

    var SaveImageToAlbum =
    {
    saveImageToAlbum:函数(成功回调,错误回调,参数)
    {
    cordova.exec(successCallback, errorCallback, "SaveImageToAlbum", "saveImageToAlbum", [args]);
    }
    };

  4. 在您的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:

  1. 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.

  1. 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.

  2. Below are the codes for the header and the implementation files:

    // MySaveImageToAlbum.h
    #import

    @interface MySaveImageToAlbum : CDVPlugin
     - (void)saveImageToAlbum:(CDVInvokedUrlCommand*)command;
    @end
    

    // MySaveImageToAlbum.m
    #import "CDVSaveImageToAlbum.h"

    #import <UIKit/UIKit.h>
    
    
    @implementation MySaveImageToAlbum
    
    - (void)saveImageToAlbum:(CDVInvokedUrlCommand*)command
    {
        CDVPluginResult* pluginResult = nil;
        NSURL *url = [NSURL URLWithString:[command.arguments objectAtIndex:0]];
        NSData *args = [NSData dataWithContentsOfURL:url];
    
    
        if (args != nil && [args length] > 0) {
    
            @try
            {
                UIImage *image = [UIImage imageWithData:args];
                NSData *imgdata = UIImagePNGRepresentation(image);
                UIImage *image2 = [UIImage imageWithData:imgdata];
                UIImageWriteToSavedPhotosAlbum(image2, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
    
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"success"];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            }
            @catch (NSException *exception) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            }
    
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
    
    }
    
    
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
      contextInfo:(void *)contextInfo
    {
        // Was there an error?
        if (error != NULL)
        {
            // Show error message...
    
        }
        else  // No errors
        {
            // Show message image successfully saved
    
        }
    }
    
    
    @end
    
  3. 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]);
    }
    };

  4. 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

荒人说梦 2024-12-30 02:01:08

我自己想出来了。我能够使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文