【问题标题】:Draw PDFPage without current context在没有当前上下文的情况下绘制 PDFPage
【发布时间】:2017-01-23 23:47:36
【问题描述】:

从 Sierra 开始,可以使用 pdf drawWithBox:toContext: 操作。但在以前的操作系统版本中,这不存在。当存在图形上下文时,前身 drawWithBox: 在任何存在上下文的情况下都能很好地工作(例如在 drawRect: 中)。但是,如果您没有这样的上下文,我看不到使用 drawWithBox: 的方法(除了采用“可能”存在的随机上下文)。我试过这个:

_contextRef =
  CGBitmapContextCreate(_cvMat.data, ... 

...

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

但那只是倾倒了

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

这不是经常遇到(或寻求)的错误消息。

【问题讨论】:

标签: objective-c pdf quartz-graphics cgcontext


【解决方案1】:

您遇到的问题是CGContextRefNSGraphicsContext 不是一回事。当您期望 NSGraphicsContext 实例时,您正在使用 CGContextRef 调用 +[NSGraphicsContext setCurrent:]

好消息是您可以很容易地从CGContext 创建NSGraphicsContext

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

看看这是否能让你在你期望的地方绘制你的 PDF。

【讨论】:

  • 你从哪里得到的初始化程序?我的 NSGraphicsContext.h 没有任何与 CGContext 相关的方法,因此编译器会发牢骚:No visible @interface for 'NSGraphicsContext' 声明了选择器 'initWithCGContext:flipped:'
  • 不管怎样,你把我引向了正确的方向,我发现stackoverflow.com/questions/10627557/… 解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 2012-07-17
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 2019-09-26
  • 2012-07-01
相关资源
最近更新 更多