简体   繁体   English

从字节数组创建CGPDFDocument

[英]Create a CGPDFDocument from a byte array

In MonoTouch I am looking to create a CGPDFDocument from a byte array transferred back text encoded from a web service. 在MonoTouch中,我希望从字节数组创建CGPDFDocument ,该字节数组传输回从Web服务编码的文本。 However MonoTouch does not seem to support creation from anything other than a local file or downloaded from a URL. 但是,MonoTouch似乎不支持从本地文件以外的其他任何内容或从URL下载的内容。

Anybody know how to do this? 有人知道该怎么做吗?

One option would be to p/invoke into CGPDFDocumentCreateWithProvider to create your own instance of a CGPDFDocument . 一种选择是p /调用CGPDFDocumentCreateWithProvider来创建自己的CGPDFDocument实例。 It should not be too complex since CGDataProvider already available in MonoTouch. 由于CGDataProvider已在MonoTouch中提供,因此它应该不太复杂。

OTOH receiving an array (from a web service) can consume a lot of memory (unless you're 100% sure about their small size). OTOH(通过Web服务)接收阵列会消耗大量内存(除非您100%确信它们的体积很小)。 Eg string to byte conversion, it must all fit in memory... 例如字符串到字节的转换,它必须全部适合内存...

TO be safe you might want to serialize the array (or better stream it directly if possible) to a local (temporary/cache) file first (to avoid another in-memory copy for the GCPDFDocument instance) and then call the existing CGPDFDocument.FromFile API. 为了安全起见,你可能想要序列化的阵列(或更好的流直接如果可能的话)到本地(临时/缓存)文件中的第一(以避免另一个内存拷贝的GCPDFDocument实例),然后调用现有CGPDFDocument.FromFile API。

UPDATE : MonoTouch 5.3.3+ will provide new constructor for GCPDFDocument that accept a CGDataProvider . UPDATE:MonoTouch的5.3.3+将用于提供新的构造GCPDFDocument接受一个CGDataProvider In the mean time you can use the following code. 同时,您可以使用以下代码。

[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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM