【问题标题】:Base64Encoded string in ios not decoding properly on server side using phpios中的Base64Encoded字符串无法在服务器端使用php正确解码
【发布时间】:2011-07-18 20:31:26
【问题描述】:

我正在使用 UIImageJPEGRepresentation(image, 1.0) 将图像转换为 NSdata,然后使用 base64 辅助类将其编码为 base64

NSString *imageOne = [self encodeBase64WithData:[imageDict objectForKey:@"ImageOne"]];

最后使用 post 方法作为 json 参数将其发送到服务器,并在服务器端使用 php 方法对其进行解码。

/* encode & write data (binary) */ 
$ifp = fopen($imageNameWithPath, "wb" ); 
fwrite($ifp, base64_decode($base64ImageString)); 
fclose($ifp); 

解码后,我将文件保存为 jpeg 图像,并以适当的大小和扩展名创建文件,但问题是当我打开它时,我得到的图像尺寸为 0X0 ..(问题就在这里)

服务器端脚本是正确的,因为我们的 android 开发人员也在发送 base64string,并且图像保存为具有适当大小和尺寸的 jpeg。

此问题仅在发送到服务器进行解码时来自 iphone 端。我已经使用我的 base64 辅助类对图像进行了解码,它在我的 iPhone 设备上运行良好。

UIImageView *viewImage = [[UIImageView alloc] initWithImage:[UIImage imageWithData: [self decodeBase64WithString:[registerDataDict objectForKey:@"imageOne"]]]];          
[self.view addSubview:viewImage];

我遵循的过程在设备上是否正确,还是我需要更改某些内容?帮助将不胜感激。

【问题讨论】:

    标签: iphone base64


    【解决方案1】:

    用于获取 base64 编码所需的 65 个额外字符有多种变体(26 个大写字母 + 26 个小写字母 + 10 个数字 = 62),并且不同的编码器可能使用不同的子集。 PHP 期望它们为['+', '/', '='],您的应用程序可能使用['-', '_', '='] 之类的东西。

    一旦你弄清楚映射是什么,你就可以在 php 中解码之前使用str_replace。 请参阅wikipedia page on base64 或php documentation for base64_decode 了解更多信息。

    【讨论】:

      【解决方案2】:

      我已成功将其与多个 OAuth 提供程序一起使用:Base64Transcoder.c 这是一个 C 类,如果您愿意,您可以轻松地将其转换为 Objective-C 类。

      我是这样编码的:

      char base64Result[32];
      size_t theResultLength = 32;
      [Base64Transcoder base64EncodeData:digest digestLength:CC_SHA1_DIGEST_LENGTH base64Result:base64Result resultLength:&theResultLength];
      NSData *theData = [NSData dataWithBytes:base64Result length:theResultLength];
      
      NSString *base64EncodedResult = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
      

      嗯,我把原始类的 C 函数 base64EncodeData 变成了一个 Objective-C 方法,但你明白了。

      【讨论】:

        猜你喜欢
        • 2016-03-18
        • 2019-08-07
        • 2016-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-25
        • 1970-01-01
        • 2018-07-02
        相关资源
        最近更新 更多