从 CGPDFPageRef 中提取缩略图
我正在尝试为 CGPDFDocument 创建缩略图。
在 Quartz 编程指南的 PDF 文档解析部分,有以下代码:
CGPDFDictionaryRef d;
CGPDFStreamRef stream; // represents a sequence of bytes
d = CGPDFPageGetDictionary(page);
// check for thumbnail data
if (CGPDFDictionaryGetStream (d, “Thumb”, &stream)){
// get the data if it exists
data = CGPDFStreamCopyData (stream, &format);
下一步是使用 data
创建图像。
Sugar在这里回答了从PDF中提取图像的类似问题: 从 PDF 中提取图像
我正在尝试使用 他的答案中列出了decodeValuesFromImageDictionary()
和getImageRef()
函数,用于创建一个UIImage来表示我的缩略图。
我的问题是,我获得的图像颜色和尺寸错误,除非我将 CGImageCreate() 函数的 CGColorSpaceRef 参数设置为 CGColorSpaceCreateDeviceGray(),在这种情况下我得到了缩略图的(正确的)灰度表示,这当然不是我想要的。
通过检查缩略图流字典,我知道图像格式是 CGPDFDataFormatRaw,ColorSpace 是 DeviceRGB。我还知道两个过滤器(ASCII85Decode 和 FlateDecode)应用于图像,尽管我不确定这是否有任何意义。
非常感谢任何关于为什么会发生这种情况以及如何解决它的建议或见解!
I am attempting to create thumbnail images for a CGPDFDocument.
In the PDF Document Parsing section of the Quartz Programming Guide, there is the following code:
CGPDFDictionaryRef d;
CGPDFStreamRef stream; // represents a sequence of bytes
d = CGPDFPageGetDictionary(page);
// check for thumbnail data
if (CGPDFDictionaryGetStream (d, “Thumb”, &stream)){
// get the data if it exists
data = CGPDFStreamCopyData (stream, &format);
The next step would be to use data
to create the image.
Sugar answers the similar question of extracting images from PDF here:
Extracting images from a PDF
I am attempting to use the decodeValuesFromImageDictionary()
and getImageRef()
functions listed in his answer to create a UIImage to represent my thumbnail.
My problem is that the image I obtain has wrong colors and wrong dimensions, except when I set the CGColorSpaceRef argument of the CGImageCreate()
function to CGColorSpaceCreateDeviceGray()
, in which case I get the (correct) greyscale representation of the thumbnail which of course is not what I want.
I know from inspecting the thumbnail stream dictionary that the image format is CGPDFDataFormatRaw and the ColorSpace is DeviceRGB. I also know that two filters (ASCII85Decode and FlateDecode) are applied to the image, even though I am not sure if this is of any significance.
Any suggestions or insight as to why this happens and what to do to fix it is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您指定的链接提供的代码中的 mona m 包含注释行:
CGColorSpaceCreateDeviceRGB 不是分析 colorSpaceArray 并获取准确的颜色空间,而是分配给 cgColorSpace 变量,这是错误的。
这是缺失方法的实现,我在互联网上找到了它。
但
不幸的是我最后在图像上出现了一些灰点。
mona m in code provided by link you specified contains commented line:
Instead of analyzing colorSpaceArray and getting accurate Color Space a CGColorSpaceCreateDeviceRGB is assigned to cgColorSpace variable which is wrong.
Here is an implementation of missing method, I found it in the internet.
}
But unfortunately I am getting some grey dots on image at the end.