如何使用PhoneGap将图像保存到iPhone照片库? [英] How Can I Save An Image To An iPhone Photo Gallery Using PhoneGap?

查看:96
本文介绍了如何使用PhoneGap将图像保存到iPhone照片库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iPhone创建一个PhoneGap应用程序,以显示图库。项目安装中包含一些图像,一些图像来自网络。当用户点击图像时,我希望他们能够将图像保存到iPhone照片库(照片)。最初,我希望应用程序让用户将图像设置为壁纸,但发现这将是非常困难或不可能的。



我只能找到一个解决方案使用PhoneGap和Objective C保存图像,但PhoneGap解决方案包含类PhoneGapCommand,已弃用。我尝试使用PGPlugin,但无法使代码工作。



我希望有人在那里可能有一个当前PhoneGap解决方案可以执行此任务,或者如果

解决方案

我知道这个问题很老了,但是我花了我一天来解决这个问题,因为这个例子不再适用于新版本的Cordova。我目前使用版本2.5.0,所以我想我会分享这个,所以其他人不必经历痛苦我做了。



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


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




name


  1. 在您的XCODE项目中,找到插件组。右键单击它,并从上下文菜单中选择新建文件...。添加一个新类文件MySaveImageToAlbum。


  2. 以下是标题和实现文件的代码:



    // MySaveImageToAlbum.h
    #import

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

    // MySaveImageToAlbum.m
    #importCDVSaveImageToAlbum.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
    {
    //有错误吗?
    if(error!= NULL)
    {
    //显示错误信息...

    }
    else //无错误
    {
    //显示消息映像已成功保存

    }
    }


    @end


  3. 接下来,您需要创建一个调用此本地代码的javascript文件。下面是代码:



    var SaveImageToAlbum =
    {
    saveImageToAlbum:function(successCallback,errorCallback,args)
    {
    cordova.exec(successCallback,errorCallback,SaveImageToAlbum,saveImageToAlbum,[args]);
    }
    };


  4. 引用您在index.html中第4版中创建的JavaScript。假设你有一个画布,并且要将其作为图像保存到相机胶卷,您可以使用其toDataURL()函数返回一个base64 PNG数据。然后可以调用saveImageToAlbum函数并将其作为参数传递,如下所示:



    SaveImageToAlbum.saveImageToAlbum(function(e){
    navigator.notification.alert Image has been successfully saved in the Photo Album。,null,Image saved!);
    },
    function(e){
    navigator.notification.alert不保存在相册中,null,保存错误!);
    },
    canvas.toDataURL());




    1. 这是它!



      希望你喜欢...



      Regards,
      Antonio C. Logarta III


      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!

      解决方案

      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

      这篇关于如何使用PhoneGap将图像保存到iPhone照片库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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