Cocoa:将CGImage转换为PDF时崩溃

时间:2015-09-07 15:40:26

标签: objective-c cocoa pdf core-graphics cgimage

我有一个基于64位Cocoa的绘图应用程序。我想使用Cocoa Drawing获取类中当前上下文的PDF表示。

使用Cocoa Drawing的类中的方法:

- (NSData*)             pdfData
{
...
    NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                                                                    pixelsWide:pixelsAcross
                                                                    pixelsHigh:pixelsDown
                                                                 bitsPerSample:8
                                                               samplesPerPixel:4
                                                                      hasAlpha:YES
                                                                      isPlanar:NO
                                                                colorSpaceName:NSCalibratedRGBColorSpace
                                                                   bytesPerRow:0
                                                                  bitsPerPixel:0];    



    NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
    [context setImageInterpolation:NSImageInterpolationHigh];
    [context setShouldAntialias:YES];

    NSGraphicsContext *nsgc = [NSGraphicsContext graphicsContextWithGraphicsPort:[context graphicsPort] flipped:YES];

    [NSGraphicsContext saveGraphicsState];

    [NSGraphicsContext setCurrentContext:nsgc];

    //custom drawing at the context
    [self drawRect:fr];

    [NSGraphicsContext restoreGraphicsState];

    CGImageRef  image = CGBitmapContextCreateImage([context graphicsPort]);

    MyPDFView* pdfView = [[MyPDFView alloc] initWithFrame:fr imageRef:image];
    NSData* pdfData = [pdfView dataWithPDFInsideRect:fr];
    return pdfData;
}

MyPDFView:的NSView

...

- (void)        drawRect:(NSRect) rect
{
    [[NSColor clearColor] set];
    NSRectFill([self bounds]);

    CGContextRef contextRef = [[NSGraphicsContext currentContext] graphicsPort];
    if (_imageRef)
        CGContextDrawImage(contextRef, CGRectMake([self bounds].origin.x, [self bounds].origin.y, NSWidth([self bounds]), NSHeight([self bounds])), _imageRef);
}

问题所在。它对我来说很完美,但我收到了一些用户的崩溃报告,我自己无法重现。

0   CoreGraphics                         0x00007fff857c2163 decode_byte_8bpc_3a + 619
1   CoreGraphics                         0x00007fff857bfdbc decode_data + 13562
2   CoreGraphics                         0x00007fff857bbe8f img_decode_read + 380
3   CoreGraphics                         0x00007fff857bba07 img_alphamerge_read + 555
4   CoreGraphics                         0x00007fff85985b82 CGSImageStreamRead + 197
5   libPDFRIP.A.dylib                    0x00000001071e08a7 PDFImageEmitDefinition + 1198
6   CoreFoundation                       0x00007fff848c0642 __CFSetApplyFunction_block_invoke + 17
7   CoreFoundation                       0x00007fff848a50b0 CFBasicHashApply + 127
8   CoreFoundation                       0x00007fff848c05ea CFSetApplyFunction + 185
9   libPDFRIP.A.dylib                    0x00000001071e1375 PDFImageSetEmitDefinitions + 54
10  libPDFRIP.A.dylib                    0x00000001071da917 emit_page_resources(PDFDocument*) + 88
11  libPDFRIP.A.dylib                    0x00000001071da89d PDFDocumentEndPage + 72
12  libPDFRIP.A.dylib                    0x00000001071d945b pdf_EndPage + 16
13  AppKit                               0x00007fff873dffca -[NSView(NSPrintingInternal) _renderCurrentPageForPrintOperation:] + 527
14  AppKit                               0x00007fff87957992 -[NSView(NSPrintingInternal) _copyForCurrentOperation] + 103
15  AppKit                               0x00007fff87784ebd -[NSConcretePrintOperation _renderView] + 177
16  AppKit                               0x00007fff87785657 -[NSConcretePrintOperation runOperation] + 677
17  AppKit                               0x00007fff879577a8 -[NSView(NSPrinting1) dataWithPDFInsideRect:] + 113
18  MyDrawingKit                         0x00000001068b9daf -[MyDrawing pdfData] (MyDrawing.m:2196)

我在这个系统方法decode_byte_8bpc_3a中搜索了崩溃的问题,并且有建议将监视器配置文件更改为“通用RGB配置文件”以避免此类崩溃,但没有解释如何在代码中避免它。

这种崩溃的原因是什么?如何避免它?

0 个答案:

没有答案