我是ios的新手,并在ios中为phonegap构建压缩图像插件。我没有得到如何在我的javascript中调用压缩图像方法。我的代码是,Plugin.h文件
- (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);
});
}
该插件被调用但是空图像。输出为空。源图像没有通过。任何人都可以帮助我, 提前致谢
答案 0 :(得分:0)
您正在正确传递源图像,但您没有在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;
}