【发布时间】:2010-08-31 11:54:04
【问题描述】:
我有两个关于在 iPad/iPhone 上处理 PDF 渲染的问题。
第一
在 iPad 上使用 CGPDF 功能渲染 PDF 页面时,我遇到了一些奇怪的问题。奇怪,因为在 iPhone 上一切正常,但在 iPad 上我收到很多与 PDF 相关的错误消息,如下所示
invalid stream length 9785; endstream not found.
FlateDecode: decoding error: incorrect header check.
invalid function: failed to read 765 bytes.
stack underflow: popping past mark.
missing or invalid arguments for color operator.
FlateDecode: decoding error: incorrect header check.
FlateDecode: decoding error.
<Error>: FT_Open_Face failed: error 2.
<Error>: FT_Open_Face failed: error 85.
encountered unexpected object type: 7.
missing or invalid object number.
invalid image `ColorSpace' value.
unexpected symbol `obj' while reading object.
invalid descendant font for Type0 font.
font `F28' not found in document.
<Error>: FT_Outline_Decompose failed: error 20.
font `R222' not found in document.
...还有很多其他的。我知道这可能是 PDF 结构错误的问题,但我不明白为什么我只能在 iPad 上看到所有这些错误,而在 iPhone 上,与 iPad 相比只有少数错误,并且在大多数情况下,页面显示正确,尽管报告的错误,这在 iPad 上又不是真的。
iPhoneOS 3.2 和 3.1/4.0 的 Quartz 实现有什么不同吗?是否有一些关于在 iPhone/iPad 上可读的 PDF 格式的指南?
第二:
另一个问题是为不同的页面大小创建位图上下文。我需要为纵向和横向渲染相同的 PDF 页面。但是,虽然它适用于纵向(都在 iPhone 上),但它不适用于 iPad 上的横向,抛出以下错误:
CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component colorspace; kCGImageAlphaPremultipliedLast; 4095 bytes/row.
用于创建上下文的代码如下:
/// Calculate memory block size needed
int bytesPerRow = (int)((int)_pageSize.width * 4);
int dataSize = (int)(bytesPerRow * _pageSize.height);
/// Allocate memory; As noted in docs, on iOS 4 it can be NULL, but as mentioned
/// below: Do not pass NULL if you are running on earlier operating systems.
/// So don't pass NULL...
void *bitmapData = malloc(dataSize);
if(!bitmapData) {
/// Hm, out of memory?
return NULL;
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(bitmapData,
_pageSize.width,
_pageSize.height,
8, /* bits per component*/
bytesPerRow, /* bytes per row */
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
if(!context) {
/// Something is really wrong!
free(bitmapData);
bitmapData = NULL;
return NULL;
}
CGContextClipToRect(context, CGRectMake(0, 0, _pageSize.width, _pageSize.height));
/// Ask data source to render contents
[_dataSource renderPageAtIndex: pageIndex inContext: context];
/// Get bitmap image from context
CGImageRef image = CGBitmapContextCreateImage(context);
/// Free context AND previously allocated block of data
CGContextRelease(context);
free(bitmapData);
bitmapData = NULL;
/// Finish operation...
正如我所说,它在 iPhone 下运行良好,并且适用于 iPad 上的纵向,但不适用于 iPad 上的横向。唯一的区别实际上是提供的当前页面大小。但是,这里的页面大小不等于横向模式下的屏幕大小(1024x768),而是计算出适合屏幕宽度的大小,允许用户垂直滚动以查看完整页面。通常,iPad 上的页面大小为 1024x1403(取决于 PDF 格式)。
我不明白为什么上面的代码不适用于更大的页面。
有什么想法吗?谢谢。
【问题讨论】: