【问题标题】:How to convert video file to Base64 string in IOS?如何在IOS中将视频文件转换为Base64字符串?
【发布时间】:2018-10-24 03:24:51
【问题描述】:

我想将本地资源文件夹视频文件 (12345.mp4) 转换为 base64 编码字符串,然后从 API 发送到服务器端,但网络团队告诉无效的 base64 编码字符串。这是正确的吗?

这是我的代码:

 NSString *base64String = @"";
 NSError *error;
 NSData *videoData;

 NSString *strVideoPath = [[NSBundle mainBundle] pathForResource:@"12345" ofType:@"mp4"];

 videoData = [[NSData alloc]initWithContentsOfFile:strVideoPath options:NSDataReadingMappedIfSafe error:&error];
 base64String = [videoData base64EncodedStringWithOptions:0];

【问题讨论】:

  • 你得到一个base64字符串吗?目前还不清楚您真正要问的是什么。
  • @rmaddy 是的,我得到了加密的 base64 字符串,但我不确定这是否是有效的 base64 字符串
  • 绝对没有加密。知道它是否有效的唯一方法是使用它。您必须有一些理由转换为 base64。继续前进,看看你的结果是否有效。
  • @rmaddy 你能把我弄错的示例代码分享给我吗?
  • 你没看错。您发布的代码很好。它创建一个 base64 字符串。

标签: ios objective-c iphone base64


【解决方案1】:

swift 版本中,您可以像这样转换视频

 *let tempURL = info[UIImagePickerController.InfoKey.mediaURL]
                let data = NSData(contentsOf: tempURL as! URL)
                print("\(String(describing: data?.length))")
              
                if data?.length ?? 0 > 0{
                      guard data != nil else {
                        return
                      }
                      let base64String = data!.base64EncodedString(options: .lineLength64Characters)
                    self.video = base64String
                }*

【讨论】:

    猜你喜欢
    • 2021-03-18
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    相关资源
    最近更新 更多