iPhone/iPad Base64->NSData->PDF 怎么办?
Obj-C 或 Monotouch.Net C# 答案都可以。
我有一个 Base64 字符串,它是通过 Web 服务接收的 PDF 文档。我可以获得 NSData。
如何获取 NSData 并将其另存为 PDF?
-- 我这样获取 NSData --
byte[] encodedDataAsBytes = System.Convert.FromBase64String (myBase64String);
string decoded = System.Text.Encoding.Unicode.GetString (encodedDataAsBytes);
NSData data = NSData.FromString (decoded, NSStringEncoding.ASCIIStringEncoding);
Obj-C or Monotouch.Net C# answers are fine.
I have a Base64 string that is a PDF document received over a web service. I can get the NSData.
How do I take the NSData and save it as a PDF?
-- I get the NSData this way --
byte[] encodedDataAsBytes = System.Convert.FromBase64String (myBase64String);
string decoded = System.Text.Encoding.Unicode.GetString (encodedDataAsBytes);
NSData data = NSData.FromString (decoded, NSStringEncoding.ASCIIStringEncoding);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保存它的最简单方法可能是使用 NSData 的 writeToFile:options:error: 方法。
The simplest way to save it is probably to use NSData's
writeToFile:options:error:
method.我发现使用 .NET 框架比尝试使用 iOS 框架更能解决这个问题。这将获取任何文件并将其转换为原始文件,然后将其保存到 iPhone/iPad 设备。 “path”只是设备上的一个文件夹。
I found that using the .NET framework works better than trying to use the iOS framework for this problem. This will take any file and convert it to it's original then save it to the iPhone/iPad device. "path" is just a folder on the dev ice.
我正在开展一个项目,最近我必须完成您所描述的同样的事情。我从 .NET Web 服务获取以字符串形式编码的 Base64 PDF 文件,这些文件需要解码为原始文件并在应用程序文档目录中另存为 PDF 文件。
我的解决方案是:
我已经测试并成功地使用此方法处理最大 25mb 的 PDF 文件。我还使用 48mb 文件进行了几次测试运行,但该文件使decodeBase64WithString 方法占用了太多内存,导致我的应用程序崩溃了。尚未找到解决方案。
如果您正在处理多个大文件,请务必偶尔释放内存。我将所有文件放在一个循环中,其中我必须使用自己的 nsautorelease 池并在循环结束时耗尽它以释放任何自动释放的对象。
I'm working on a project where I recently had to accomplish the same thing you are describing. I get base64 encoded PDF files as strings from a .NET web service which need to be decoded to their original and saved as PDF files in the applications documents directory.
My solution was:
I have tested and successfully used this method with PDF files that are up to 25mb. I also had a couple of test runs with a 48mb file but that file made the decodeBase64WithString method take up too much memory and my app crashed. Haven't found a solution to this yet..
If you are working with multiple large files be sure to free up your memory once in a while. I got all my files in one loop in which I had to use my own nsautorelease pool and drain it at the end of the loop to free up any autoreleased objects.