2015-06-19 100 views
1

我使用的base64相互转换为字符串,而我需要它作为映像文件的路径在iOS原生插件base64转换图像中的iOS编程文件路径图像

像这样的事情在Android中被转换位图文件I它需要在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. 

任何人都可以请让我知道我要去哪里wrong..trying以Base64格式解码和图像到实际文件

+0

如果我在NSString中发送空数据* jsCallBack = [NSString stringWithFormat:@“%@(%@);”,@“profileBase64Callback”,@“”];而不是上面代码中的strCOnvert,比我的回调方法被称为..console == citizenVal.profileImage:undefined – Sujania

回答

1

这下面的代码解决了我的问题:

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

    }); 

}