2017-01-23 104 views
1

从Sierra the's pdf drawWithBox:toContext:开始可用操作。但在以前的操作系统版本中,这不存在。当存在图形背景时,前任drawWithBox:在存在上下文的情况下工作得很好(例如,在drawRect:中)。但是如果你没有这样的背景,我就没有办法使用drawWithBox:(除了采用“可能”存在的随机上下文)。我尝试这样做:在没有当前上下文的情况下绘制PDFPage

_contextRef = 
    CGBitmapContextCreate(_cvMat.data, ... 

... 

if (v12) { 
    [page drawWithBox:kPDFDisplayBoxBleedBox toContext:cgContext]; 
} else { 
    [NSGraphicsContext setCurrentContext:(__bridge NSGraphicsContext * _Nullable)(cgContext)]; 
    [page drawWithBox:kPDFDisplayBoxBleedBox]; 
} 

但只是把

- [__ NSCFType graphicsPort]:无法识别的选择发送到实例0x7f8de1e219a0

这是不是一个错误消息中遇到(或追捧) 常常。

+0

[Mac OS X:使用CGContextRef C函数绘制到屏幕外NSGraphicsContext中可能有重复,但不起作用。为什么?](http://stackoverflow.com/questions/10627557/mac-os-x-drawing-into-an-offscreen-nsgraphicscontext-using-cgcontextref-c-funct) - 肯定这是一个重复的:-) –

回答

0

您遇到的问题是CGContextRefNSGraphicsContext不是一回事。当您期待NSGraphicsContext实例时,您打电话给+[NSGraphicsContext setCurrent:]并输入CGContextRef

好消息是,你可以从一个CGContext很容易一NSGraphicsContext

CGContextRef cgContext; 
NSGraphicsContext *newContext = [[NSGraphicsContext alloc] initWithCGContext:cgContext flipped:NO]; 
[NSGraphicsContext setCurrent: newContext]; 

看看是否让你画你的PDF,你的期望。

+0

你从哪里得到初始化程序?我的NSGraphicsContext.h没有任何与CGContext相关的方法,因此编译器声明:_No visible @interface for'NSGraphicsContext'声明了选择器'initWithCGContext:flip:'_ –

+0

无论如何,你引导我朝着正确的方向发现http ://stackoverflow.com/questions/10627557/mac-os-x-drawing-into-an-offscreen-nsgraphicscontext-using-cgcontextref-c-funct它解决了我的问题。 –