Javascript 在phonegap中压缩ios图像插件
我是ios新手,在ios中为phonegap构建压缩图像插件。我不知道如何在javascript中调用压缩图像方法。我的代码是Plugin.h文件Javascript 在phonegap中压缩ios图像插件,javascript,jquery,ios,cordova,Javascript,Jquery,Ios,Cordova,我是ios新手,在ios中为phonegap构建压缩图像插件。我不知道如何在javascript中调用压缩图像方法。我的代码是Plugin.h文件 - (void) cordovaCompressimg:(CDVInvokedUrlCommand *)command; Plugin.m文件 - (void) cordovaCompressimg:(CDVInvokedUrlCommand *)command { UIImage *sourceImage ; NSStri
- (void) cordovaCompressimg:(CDVInvokedUrlCommand *)command;
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文件
window.showimg = function(cimg, callback) {
cordova.exec(callback, function(err) {
callback('Nothing to echo.');
}, "PhonegapPlugin", "cordovaGetCurrentDate", [cimg]);
};
我在index.html中的调用函数是
function showCompressimg() {
window.showimg("", function(echoValue)
{
alert(echoValue);
});
}
插件被调用,但图像为空。输出为空。源图像未通过。有人能帮我吗,
提前感谢您正确地传递了源图像,但没有检索在objective-c代码中的参数中传递的图像。无论何时通过插件Javascript传递任何参数,这些参数都存储在CDVInvokedUrlCommand实例命令中。所以你可以像这样修改你的代码-
- (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;
}