简体   繁体   English

在iOS编程中将Base64图像转换为文件路径图像

[英]Convert Base64 image to file path image in iOS programming

I am using conversion from base64 to a String while I need it as image file path in iOS native plugin 我正在使用从base64到String的转换,而我需要将它用作iOS本机插件中的图像文件路径

Something like this in android which is converting bitmap to file I need its alternative in iOS 像这样在android中将位图转换为文件,我需要在iOS中使用它的替代方法

 private String convertBitmapToFile(Bitmap photo)

As my code conversion for base64 to string is not calling a call back method " profileBase64Callback " 由于我将base64转换为字符串的代码未调用回调方法“ profileBase64Callback

 plugin call://here citizen.profileImage is in base64 format
 myPlugin.convertToFile("profileBase64Callback", citizen.profileImage,  function(){ }, function(){ });

Nativeplugin.m 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];

});

calling a javascript method to execute: 调用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.

Can anyone please let me know where am I going wrong..trying to decode and Image in base64 format to an actual file 任何人都可以让我知道我要去哪里了..尝试将base64格式的图像和图像解码为实际文件

This below code solved my issue: 下面的代码解决了我的问题:

- (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];

    });

 }

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

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