我有两个与在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格式)。
我不明白为什么上面的代码不适用于更大的页面大小。
请问,好吗?感谢。
答案 0 :(得分:3)
关于第二个问题,我只是遇到了类似的问题。当您使用4字节像素时警告表示您每行分配4095个字节这一事实表明您可能无法在乘法之前正确转换为int。
对我来说,我能够通过相当于
来解决它int bytesPerRow = (int)((int)_pageSize.width * 4);
到
int bytesPerRow = lrintf(_pageSize.width) * 4;