在iOS编程中将Base64图像转换为文件路径图像
Convert Base64 image to file path image in iOS programming
我正在使用从base64到字符串的转换,而我需要它作为iOS本机插件中的图像文件路径
在 android 中像这样的东西正在将位图转换为文件,我需要它在 iOS 中的替代品
private String convertBitmapToFile(Bitmap photo)
由于我将 base64 转换为字符串的代码没有调用回调方法"profileBase64Callback"
plugin call://here citizen.profileImage is in base64 format
myPlugin.convertToFile("profileBase64Callback", citizen.profileImage, function(){ }, function(){ });
Nativeplugin.m
- (void)convertToFile:(CDVInvokedUrlCommand *)command{
NSDictionary* options = [[NSDictionary alloc]init];
if ([command.arguments count] > 0) {
options = [command argumentAtIndex:0];
NSString *base64Data =[options objectForKey:@"base64"];
NSLog(@"strttrtr :%@", base64Data);
}
NSData* data = [[NSData alloc] initWithBase64EncodedString: base64Data options:0];
NSString *strCOnvert = [NSString base64StringFromData:data length:[data length]];//Instead of this string I want decoded format(file path) to send to javascript Or if any other format which fixes my issue
dispatch_async(dispatch_get_main_queue(), ^{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:strCOnvert];
[pluginResult setKeepCallbackAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
NSString * jsCallBack = [NSString stringWithFormat:@"%@(%@);", @"profileBase64Callback", strCOnvert];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
});
调用 JavaScript 方法执行:
//Need this imageURI after conversion which I am unable to convert
function profileBase64Callback(imageURI){
console.log("profileImage path " );
//updating to database
updateCitizenValue("profileImage", imageURI);
setTimeout(getCitizen(function(citizenVal){
}), 5000);
}//this method is not called due to error in conversion.
谁能让我知道我哪里出错了..尝试将 base64 格式的图像解码和图像转换为实际文件
下面的代码解决了我的问题:
- (void)convertToFile:(CDVInvokedUrlCommand *)command{
NSDictionary* options = [[NSDictionary alloc]init];
NSString *base64String;
if ([command.arguments count] > 0) {
options = [command argumentAtIndex:0];
base64String = [options objectForKey:@"base64"];
methodname1 =[options objectForKey:@"callback"];
}
NSData* data = [[NSData alloc] initWithBase64EncodedString:methodname1 options:0];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
/*
identify the home directory and file name
*/
NSLog(@"----jpgPath--%@",jpgPath);
[data writeToFile:jpgPath atomically:YES];
/*
Send resulting data to cordova plugin
*/
dispatch_async(dispatch_get_main_queue(), ^{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"OK"];
[pluginResult setKeepCallbackAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
NSString * jsCallBack = [NSString stringWithFormat:@"%@(%@);", @"profileBase64Callback", jpgPath];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
});
}
相关文章:
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 如何在内联依赖项并将图像转换为dataURI的情况下完全提取网页
- 无法在Cordova 4.0 ios中将jpg/jpeg图像转换为Base64
- ng文件上传获胜't将jpg图像转换为png
- 画架JS透视图像转换
- 将普通的 PNG 图像转换为 JSON 数据,然后存储到对象
- 如何使用javascript将图像转换为字符串
- 将SVG过滤器应用的图像转换为二进制或base64以保存在后端
- FS.Store 不可靠的GM图像转换
- javascript 将 IMG 标签中的图像转换为 标签中的实际链接图像
- 在JavaScript IE8中将图像转换为字节代码
- 如何在 JSP/JavaScript 中将图像转换为字节数组
- 如何在 JavaScript 中将图像转换为 base64
- 使用GraphicsMagick和Node.js将多个图像转换为多页pdf文件
- 图像转换的 URL
- 在javascript中将base64图像转换为文件,或者如何使用jquery ajax传递一个大的base64字符串
- 图像转换错误裁剪
- 在 JQuery 中将图像转换为 base64string
- 在 Javascript 中将图像转换为 RGB 数组
- 将相机捕获图像转换为 base64 - Ionic