简体   繁体   English

在Phonegap中为iOS压缩图像插件

[英]Compress image plugin for ios in phonegap

I am new to ios and building compress image plugin in ios for phonegap.I am not getting how to call the compress image method in my javascript. 我是ios新手,正在ios中为phonegap构建compress图像插件。我没有获得如何在JavaScript中调用compress image方法的信息。 My code is, Plugin.h file 我的代码是Plugin.h文件

- (void) cordovaCompressimg:(CDVInvokedUrlCommand *)command;

Plugin.m file Plugin.m文件

- (void) cordovaCompressimg:(CDVInvokedUrlCommand *)command 
 {
     UIImage *sourceImage ;
     NSString *compimg =[self UIImageToBaseSixtyFour];
CDVPluginResult *pluginResult = [ CDVPluginResult
                             resultWithStatus    : CDVCommandStatus_OK
                             NSData : compimg
                             ];

 [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}


 -(NSData *)UIImageToBaseSixtyFour
  {
    UIImage *sourceImage ;
    NSData *imageData = UIImageJPEGRepresentation(sourceImage, 1.0);

    NSString *base64 = [NSString stringWithFormat:@"%@",[UIImageJPEGRepresentation(sourceImage, 0.95) base64EncodedString]];

    return base64;
}

plugin.js file plugin.js文件

window.showimg = function(cimg, callback) {
           cordova.exec(callback, function(err) {
          callback('Nothing to echo.');
          }, "PhonegapPlugin", "cordovaGetCurrentDate", [cimg]);
 };

my calling function in index.html is, 我在index.html中的调用函数是

        function showCompressimg() {
                  window.showimg("", function(echoValue) 
                  {
                       alert(echoValue);

                  });
        }

The plugin is getting called but with empty image.The out put comes as null.The source image is not getting passed.Can anybody help me please, Thanks in advance 插件被调用但图像为空。输出为null。源图像未传递。任何人都可以帮助我,谢谢

You are passing the source image correctly, but you are not retrieving the image passed in your argument in objective-c code. 您正在正确传递源图像,但没有检索在Objective-C代码中的参数中传递的图像。 Whenever you pass any argument via plugin Javascript, those arguments are stored in CDVInvokedUrlCommand instance command. 每当您通过插件Javascript传递任何参数时,这些参数都存储在CDVInvokedUrlCommand实例命令中。 So you can modify your code like this - 因此,您可以像这样修改代码-

- (void) cordovaCompressimg:(CDVInvokedUrlCommand *)command 
 {
     //Arguments are passed in an Array. Source Image is present at Index 0
     UIImage *sourceImage  = [command argumentAtIndex:0];
     NSString *compimg =[self UIImageToBaseSixtyFour:sourceImage];
CDVPluginResult *pluginResult = [ CDVPluginResult
                             resultWithStatus    : CDVCommandStatus_OK
                             NSData : compimg
                             ];

 [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}


 -(NSData *)UIImageToBaseSixtyFour:(UIImage *)img
  {
    //UIImage *sourceImage ;
    NSData *imageData = UIImageJPEGRepresentation(img, 1.0);

    NSString *base64 = [NSString stringWithFormat:@"%@",[UIImageJPEGRepresentation(img, 0.95) base64EncodedString]];

    return base64;
}

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

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