2017-01-23 131 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它解決了我的問題。 –

相關問題