3

在 JSON 文件中,我收到了 base64 编码的字符串。它可能包含图像文件、pdf 文件、doc 或文本文件或音频/视频文件。

如何在 iOS 中从 base64 编码的字符串显示/播放音频/视频?

我为图像、文档、pdf、文本文件(音频/视频文件除外)实现了代码。我使用下面的代码来实现这一点,

//for image

NSURL *url = [NSURL URLWithString:base64String];    
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *ret = [UIImage imageWithData:imageData];

//for ppt, pdf, doc, and textfile

    NSData* myData = [NSData dataFromBase64String: base64EncodedString];
    [_webView loadData:myData
                 MIMEType:@"application/pdf"
         textEncodingName:@"NSUTF8StringEncoding"
                  baseURL:[NSURL URLWithString:@"https://www.google.co.in/"]];
4

1 回答 1

3
//try this simple logic as per your code

NSURL *URL = [NSURL URLWithString:
                              [NSString stringWithFormat:@"data:audio/mp3;base64,%@",
                               yourBase64EncodedString]];


 [_webView loadRequest:[NSURLRequest requestWithURL:URL]];
于 2018-04-02T13:23:01.587 回答