Objective c 在iOS中生成第一个PDF页面的图像
我有一个iPad系统,可以保存大量PDF文件。对于每个PDF文件,我生成第一页的图像(以支持UI中的可视索引) 我用来生成图像的代码已经成功地用于数千个不同的PDF文件。但是,我现在遇到了一个PDF文件,它在Adobe Reader中可以正常打开,但在我尝试在iOS中生成图像时,它会导致错误的访问异常。导致问题的实际文档如下所示: 我用于生成图像的代码如下所示:Objective c 在iOS中生成第一个PDF页面的图像,objective-c,pdf,uiimage,cgcontextref,cgpdfdocumentref,Objective C,Pdf,Uiimage,Cgcontextref,Cgpdfdocumentref,我有一个iPad系统,可以保存大量PDF文件。对于每个PDF文件,我生成第一页的图像(以支持UI中的可视索引) 我用来生成图像的代码已经成功地用于数千个不同的PDF文件。但是,我现在遇到了一个PDF文件,它在Adobe Reader中可以正常打开,但在我尝试在iOS中生成图像时,它会导致错误的访问异常。导致问题的实际文档如下所示: 我用于生成图像的代码如下所示: +(void)createLiteratureImageWithMeta:(HPSLiteratureMeta*)literature
+(void)createLiteratureImageWithMeta:(HPSLiteratureMeta*)literatureMeta andData:(NSData*)literatureData
{
// Used to create a .png image file of the first page of a pdf document.
// The image is written to the file system, and has a name of the literatureId for the corresponding literature coredata record.
CFDataRef myPDFData = (__bridge CFDataRef)literatureData;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
if (pdf)
{
CGPDFPageRef PDFPage = CGPDFDocumentGetPage(pdf, 1);
if (PDFPage)
{
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 210, 297)]; // A4 = 210mm x 297mm
// Determine the size of the PDF page.
CGRect pageRect = CGPDFPageGetBoxRect(PDFPage, kCGPDFMediaBox);
CGFloat PDFScale = view.frame.size.width/pageRect.size.width;
pageRect.size = CGSizeMake(pageRect.size.width*PDFScale, pageRect.size.height*PDFScale);
/*
Create a low resolution image representation of the PDF page to display before the TiledPDFView renders its content.
*/
UIGraphicsBeginImageContext(pageRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// First fill the background with white.
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,pageRect);
CGContextSaveGState(context);
// Flip the context so that the PDF page is rendered right side up.
CGContextTranslateCTM(context, 0.0, pageRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Scale the context so that the PDF page is rendered at the correct size for the zoom level.
CGContextScaleCTM(context, PDFScale,PDFScale);
CGContextDrawPDFPage(context, PDFPage);
CGContextRestoreGState(context);
UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
backgroundImageView.frame = pageRect;
backgroundImageView.contentMode = UIViewContentModeScaleAspectFit;
[view addSubview:backgroundImageView];
[view sendSubviewToBack:backgroundImageView];
NSData* imageData = UIImagePNGRepresentation(backgroundImageView.image);
NSString* filename = [NSString stringWithFormat:@"%@.png",literatureMeta.id];
filename = [HPSFileHelper encodeFilename:filename];
[HPSFileHelper writeDataToFileWithData:imageData andFilename:filename];
}
CGPDFDocumentRelease (pdf);
CGDataProviderRelease (provider);
}
pdf = nil;
provider = nil;
}
将为以下行(从开始向下大约47行)引发异常:
它在这条线上持续崩溃
坠机情况如下所示:
Thread : Crashed: NSOperationQueue 0x1562f060
0 ImageIO 0x2e254032 transfer_fixed_point(kdu_line_buf*, int, int, unsigned char*, int, bool) + 477
1 ??? 0x00001010
2 ImageIO 0x2e2537d3 kdu_region_decompressor::process_generic(int, int, kdu_coords, int, int, int, kdu_dims&, kdu_dims&, int, bool) + 8570
3 ImageIO 0x2e251651 kdu_region_decompressor::process(unsigned char**, bool, int, kdu_coords, int, int, int, kdu_dims&, kdu_dims&, int, bool) + 208
4 ImageIO 0x2e247365 kdrc_stream::process(int, kdu_dims&, int&) + 588
5 ImageIO 0x2e24db69 kdu_region_compositor::process(int, kdu_dims&) + 268
6 ImageIO 0x2e2b5ef3 _cg_JP2DecompressBlock + 374
7 ImageIO 0x2e2b76b9 copyImageBlockSetJP2 + 1016
8 ImageIO 0x2e17f71f ImageProviderCopyImageBlockSetCallback + 542
9 CoreGraphics 0x2d4c545d CGImageProviderCopyImageBlockSetWithOptions + 136
10 CoreGraphics 0x2d4e817b CGImageProviderCopyImageBlockSet + 26
11 CoreGraphics 0x2d4c512b img_blocks_create + 406
12 CoreGraphics 0x2d4e814b img_blocks_extent + 62
13 CoreGraphics 0x2d4e8105 img_interpolate_extent + 108
14 CoreGraphics 0x2d4b9179 img_data_lock + 4408
15 CoreGraphics 0x2d4b783d CGSImageDataLock + 88
16 libRIP.A.dylib 0x2d803e67 ripc_AcquireImage + 98
17 libRIP.A.dylib 0x2d80320d ripc_DrawImage + 588
18 CoreGraphics 0x2d4b7753 CGContextDelegateDrawImage + 50
19 CoreGraphics 0x2d4b75e9 CGContextDrawImage + 284
20 CoreGraphics 0x2d5776b3 draw_image + 82
21 CoreGraphics 0x2d57764b CGPDFDrawingContextDrawImage + 218
22 CoreGraphics 0x2d537831 op_Do + 96
23 CoreGraphics 0x2d551419 pdf_scanner_handle_xname + 76
24 CoreGraphics 0x2d550fa7 CGPDFScannerScan + 202
25 CoreGraphics 0x2d54bbc3 CGPDFDrawingContextDrawGroupStream + 194
26 CoreGraphics 0x2d59df4d CGPDFDrawingContextDrawGroup + 284
27 CoreGraphics 0x2d53781f op_Do + 78
28 CoreGraphics 0x2d551419 pdf_scanner_handle_xname + 76
29 CoreGraphics 0x2d550fa7 CGPDFScannerScan + 202
30 CoreGraphics 0x2d54b7e7 CGPDFDrawingContextDrawPage + 358
31 CoreGraphics 0x2d59d7c1 pdf_page_draw_in_context + 88
32 CoreGraphics 0x2d57e541 CGContextDrawPDFPage + 36
33 HPS 0x00060737 +[HPSImageHelper createLiteratureImageWithMeta:andData:] (HPSImageHelper.m:690)
34 HPS 0x00092d0d -[HPSDbOperationFileDownload doTheWorkWithMOC:] (HPSDbOperationFileDownload.m:63)
35 HPS 0x00070eb1 -[HPSDbOperationBase main] (HPSDbOperationBase.m:80)
36 Foundation 0x2dd6c7db -[__NSOperationInternal _start:] + 770
37 Foundation 0x2de10995 __NSOQSchedule_f + 60
38 libdispatch.dylib 0x380bd68f _dispatch_async_redirect_invoke$VARIANT$mp + 110
39 libdispatch.dylib 0x380bed71 _dispatch_root_queue_drain + 220
40 libdispatch.dylib 0x380bef59 _dispatch_worker_thread2 + 56
41 libsystem_pthread.dylib 0x381f9dbf _pthread_wqthread + 298
有人能看到代码的问题吗(到目前为止,它已经在数千个其他文档中运行良好)?有人能检测到实际PDF的问题吗
非常感谢。PSPDFKit的开发人员似乎也遇到了类似的问题,而且似乎要追溯到苹果的一些代码,这些代码在iOS 7.1 Beta 1发布后得到了修复 以下是他们讨论该问题的门票链接:
我建议在iOS 7.1 Beta 1或更高版本上运行它,看看问题是否仍然存在。或者,尝试他们在罚单上的建议,并通过预览重新保存pdf,以查看是否修复了它。我看不出实际pdf文件中有任何错误-即使使用callas pdfToolbox生成PNG图像,它也会通过预飞行并进行无误渲染。所以看起来文件本身至少是可以接受的。看起来是JPEG2000图像解压器出了问题;你是在模拟器上还是在实际设备上看到的?可能是内存问题吗?@davidvandriesche这发生在多个物理设备上,当时内存使用率很低。非常感谢您的关注。
Thread : Crashed: NSOperationQueue 0x1562f060
0 ImageIO 0x2e254032 transfer_fixed_point(kdu_line_buf*, int, int, unsigned char*, int, bool) + 477
1 ??? 0x00001010
2 ImageIO 0x2e2537d3 kdu_region_decompressor::process_generic(int, int, kdu_coords, int, int, int, kdu_dims&, kdu_dims&, int, bool) + 8570
3 ImageIO 0x2e251651 kdu_region_decompressor::process(unsigned char**, bool, int, kdu_coords, int, int, int, kdu_dims&, kdu_dims&, int, bool) + 208
4 ImageIO 0x2e247365 kdrc_stream::process(int, kdu_dims&, int&) + 588
5 ImageIO 0x2e24db69 kdu_region_compositor::process(int, kdu_dims&) + 268
6 ImageIO 0x2e2b5ef3 _cg_JP2DecompressBlock + 374
7 ImageIO 0x2e2b76b9 copyImageBlockSetJP2 + 1016
8 ImageIO 0x2e17f71f ImageProviderCopyImageBlockSetCallback + 542
9 CoreGraphics 0x2d4c545d CGImageProviderCopyImageBlockSetWithOptions + 136
10 CoreGraphics 0x2d4e817b CGImageProviderCopyImageBlockSet + 26
11 CoreGraphics 0x2d4c512b img_blocks_create + 406
12 CoreGraphics 0x2d4e814b img_blocks_extent + 62
13 CoreGraphics 0x2d4e8105 img_interpolate_extent + 108
14 CoreGraphics 0x2d4b9179 img_data_lock + 4408
15 CoreGraphics 0x2d4b783d CGSImageDataLock + 88
16 libRIP.A.dylib 0x2d803e67 ripc_AcquireImage + 98
17 libRIP.A.dylib 0x2d80320d ripc_DrawImage + 588
18 CoreGraphics 0x2d4b7753 CGContextDelegateDrawImage + 50
19 CoreGraphics 0x2d4b75e9 CGContextDrawImage + 284
20 CoreGraphics 0x2d5776b3 draw_image + 82
21 CoreGraphics 0x2d57764b CGPDFDrawingContextDrawImage + 218
22 CoreGraphics 0x2d537831 op_Do + 96
23 CoreGraphics 0x2d551419 pdf_scanner_handle_xname + 76
24 CoreGraphics 0x2d550fa7 CGPDFScannerScan + 202
25 CoreGraphics 0x2d54bbc3 CGPDFDrawingContextDrawGroupStream + 194
26 CoreGraphics 0x2d59df4d CGPDFDrawingContextDrawGroup + 284
27 CoreGraphics 0x2d53781f op_Do + 78
28 CoreGraphics 0x2d551419 pdf_scanner_handle_xname + 76
29 CoreGraphics 0x2d550fa7 CGPDFScannerScan + 202
30 CoreGraphics 0x2d54b7e7 CGPDFDrawingContextDrawPage + 358
31 CoreGraphics 0x2d59d7c1 pdf_page_draw_in_context + 88
32 CoreGraphics 0x2d57e541 CGContextDrawPDFPage + 36
33 HPS 0x00060737 +[HPSImageHelper createLiteratureImageWithMeta:andData:] (HPSImageHelper.m:690)
34 HPS 0x00092d0d -[HPSDbOperationFileDownload doTheWorkWithMOC:] (HPSDbOperationFileDownload.m:63)
35 HPS 0x00070eb1 -[HPSDbOperationBase main] (HPSDbOperationBase.m:80)
36 Foundation 0x2dd6c7db -[__NSOperationInternal _start:] + 770
37 Foundation 0x2de10995 __NSOQSchedule_f + 60
38 libdispatch.dylib 0x380bd68f _dispatch_async_redirect_invoke$VARIANT$mp + 110
39 libdispatch.dylib 0x380bed71 _dispatch_root_queue_drain + 220
40 libdispatch.dylib 0x380bef59 _dispatch_worker_thread2 + 56
41 libsystem_pthread.dylib 0x381f9dbf _pthread_wqthread + 298