【问题标题】:Create a CGPDFDocument from a byte array从字节数组创建 CGPDFDocument
【发布时间】:2012-04-05 10:01:57
【问题描述】:

在 MonoTouch 中,我希望从一个字节数组中创建一个 CGPDFDocument,该字节数组传输回从 Web 服务编码的文本。然而,MonoTouch 似乎不支持从本地文件或从 URL 下载以外的任何内容创建。

有人知道怎么做吗?

【问题讨论】:

    标签: c# ios xamarin.ios bytearray cgpdfdocument


    【解决方案1】:

    一种选择是 p/invoke 到 CGPDFDocumentCreateWithProvider 以创建您自己的 CGPDFDocument 实例。它不应该太复杂,因为 CGDataProvider 已经在 MonoTouch 中可用。

    OTOH(来自 Web 服务)接收数组会消耗大量内存(除非您 100% 确定它们的小尺寸)。例如。字符串到字节的转换,它必须都适合内存...

    为了安全起见,您可能希望先将数组序列化(或最好直接将其流式传输)到本地(临时/缓存)文件(以避免GCPDFDocument 实例的另一个内存副本),然后调用现有的CGPDFDocument.FromFile API。

    更新:MonoTouch 5.3.3+ 将为GCPDFDocument 提供接受CGDataProvider 的新构造函数。同时,您可以使用以下代码。

    [DllImport (Constants.CoreGraphicsLibrary)]
    extern static IntPtr CGPDFDocumentCreateWithProvider (IntPtr provider);
    
    static GCPDFDocument FromArray (byte[] array)
    {
        using (var dp = new CGDataProvider (array, 0, array.Length) {
            return new CGPDFDocument (CGPDFDocumentCreateWithProvider (dp.Handle));
        }
    }
    

    【讨论】:

    • 非常感谢 - 事实上我最近发现将 PDF 字节数组直接加载到 UIWebView 中非常容易
    猜你喜欢
    • 2011-10-05
    • 2015-11-19
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 2011-04-11
    相关资源
    最近更新 更多