首页 > 解决方案 > 如何在IOS中将视频文件转换为Base64字符串?

问题描述

我想将本地资源文件夹视频文件(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];

标签: iosobjective-ciphonebase64

解决方案


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
                }*

推荐阅读