【问题标题】:UIGraphicsGetCurrentContext seems to return nilUIGraphicsGetCurrentContext 似乎返回 nil
【发布时间】:2012-09-07 15:05:27
【问题描述】:

我在这里尝试将单个 PDF 页面转换为 PNG,它一直运行良好,直到 UIGraphicsGetCurrentContext 突然开始返回 nil。

我正在尝试在这里追溯我的步骤,但我不太确定我是否知道这发生在什么时候。我的帧不是 0,我认为这可能会造成这个问题,但除此之外,一切“看起来”都是正确的。

这是我的代码的开头。

_pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)_pdfFileUrl);
CGPDFPageRef myPageRef = CGPDFDocumentGetPage(_pdf, pageNumber);
CGRect aRect = CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox);
CGRect bRect = CGRectMake(0, 0, height / (aRect.size.height / aRect.size.width), height);
UIGraphicsBeginImageContext(bRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

有人知道还有什么可能导致 nil 上下文吗?

【问题讨论】:

  • 这段代码在哪里?在什么物体上?

标签: ios pdf


【解决方案1】:

不必从“drawRect”调用。 你也可以在“UIGraphicsBeginImageContext(bRect.size);”之后调用它

检查下一行

UIGraphicsBeginImageContext(bRect.size);

如果 bRect.size 不是 0,0

在我的例子中,这就是下一行返回的上下文为空的原因。

【讨论】:

  • 是的,我的问题是尺寸是 CGSizeZero
  • 请注意,如果高度或宽度为 0,也会发生这种情况,而不仅仅是 0 尺寸的情况。例如,如果你的宽度是 375 而你的高度是 0,那么上下文仍然是 nil。
  • 这也是我的问题
【解决方案2】:

你是在 drawRect 方法中调用 UIGraphicsGetCurrentContext() 吗?据我所知,只能在drawRect内部调用,否则只会返回nil。

【讨论】:

  • 是的,这个问题已经在 Stack Overflow 上回答过好几次了!
  • 仍然不确定是什么原因造成的,但我在这里使用了别人的 PDF 来对类进行图像处理并修复了它。也使用 UIGraphicsGetCurrentContext 而不是在 drawRect 中,它在那里工作正常。
  • @borrrden 你能给我一些类似问题的链接吗?
【解决方案3】:

确实,CGContextRef 对象在drawRect 方法中设置后可以重用。 关键是 - 您需要在从任何地方使用 Context 之前将其推送到堆栈。否则,当前上下文将为 0x0
1.添加

@interface RenderView : UIView {
    CGContextRef visualContext;
    BOOL renderFirst;
}


2.在您的@implementation 中,在视图出现在屏幕上之前先将 renderFirst 设置为 TRUE,然后:

-(void) drawRect:(CGRect) rect {
  if (renderFirst) {
      visualContext = UIGraphicsGetCurrentContext();
      renderFirst = FALSE;
  }
}


3。在设置上下文后将某些内容渲染到上下文。

-(void) renderSomethingToRect:(CGRect) rect {
   UIGraphicsPushContext(visualContext);
   // For instance
   UIGraphicsPushContext(visualContext);
   CGContextSetRGBFillColor(visualContext, 1.0, 1.0, 1.0, 1.0);
   CGContextFillRect(visualContext, rect);
}


这是一个与线程案例完全匹配的示例:

- (void) drawImage: (CGImageRef) img inRect: (CGRect) aRect {
    UIGraphicsBeginImageContextWithOptions(aRect.size, NO, 0.0);
    visualContext = UIGraphicsGetCurrentContext();
    CGContextConcatCTM(visualContext, CGAffineTransformMakeTranslation(-aRect.origin.x, -aRect.origin.y));
    CGContextClipToRect(visualContext, aRect);
    CGContextDrawImage(visualContext, aRect, img);
    // this can be used for drawing image on CALayer
    self.layer.contents = (__bridge id) img;
    [CATransaction flush];
    UIGraphicsEndImageContext();
}


并从之前在这篇文章中拍摄的上下文中绘制图像:

-(void) drawImageOnContext: (CGImageRef) someIm onPosition: (CGPoint) aPos {
    UIGraphicsPushContext(visualContext);
    CGContextDrawImage(visualContext, CGRectMake(aPos.x,
                    aPos.y, someIm.size.width,
                    someIm.size.height), someIm.CGImage);
}

在需要上下文来呈现对象之前,请勿调用 UIGraphicsPopContext() 函数。
当调用方法完成时,似乎 CGContextRef 正在从图形堆栈的顶部自动删除。
无论如何,这个例子似乎是一种 Hack——不是 Apple 计划和提出的。该解决方案非常不稳定,并且仅适用于屏幕顶部的一个 UIView 内的直接方法消息调用。在“执行选择”调用的情况下,上下文不会将任何结果呈现到屏幕上。因此,我建议使用 CALayer 作为屏幕目标的渲染,而不是直接使用图形上下文。
希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-18
    • 2021-01-19
    • 2015-09-17
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    相关资源
    最近更新 更多