从 PDFDocument 中获取 CGImageRef
我有一个 PDFDocument(当然由 PDFPages 组成),我需要一个 CGImageRef 来粘贴到我的 IKImageView 中。
目前,我从 PDFPage 获取数据表示,将其放入 NSImage,然后获取 NSImage 的 TIFFRepresentation,将其放入 CGImageSource,然后从源中获取 CGImage。
这似乎不必要地复杂,要经过两个 NSData 步骤...
但是,当我尝试将 PDF 数据直接放入 CGImageSource 中时,即使这看起来可行,当我取出 CGImage 时,它始终为 NULL。我应该做一些具体的事情才能让它发挥作用吗?
I have a PDFDocument (made up of PDFPages ofcourse) and I need a CGImageRef to stick into my IKImageView.
Currently, I get the datarepresentation from the PDFPage, put it into an NSImage, then get the TIFFRepresentation of the NSImage, put it into a CGImageSource, and then get the CGImage out of the source.
That seemsneedlessly complicated, going through two NSData steps...
However, when I try to put the PDF data directly into the CGImageSource, even though that APPEARS to work, when I get the CGImage out, it is always NULL. is there something specific I should be doing to get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Snow Leopard 中,有一个方法
-[NSImage CGImageForProposedRect:context:hints:]
。或者您可以将页面绘制到 CGBitmapContext 中并使用 CGBitmapContextCreateImage 。In Snow Leopard there is a method
-[NSImage CGImageForProposedRect:context:hints:]
. Or you could draw the page into a CGBitmapContext and useCGBitmapContextCreateImage
.看看我的 中的
CreatePDFPageImage
函数CGPDFAdditions 代码。我认为这正是您所需要的。Look at the
CreatePDFPageImage
function in my CGPDFAdditions code. I think that's exactly what you need.尝试将 PDF 文档(而不是单个页面)的数据输入 CGImageSource。来源中的图像应与文档中的页面相对应。
Try feeding the data for the PDF document (as opposed to a single page) into the CGImageSource. Images from the source should correspond to pages in the document.