UIGraphicsGetCurrentContext似乎返回nil [英] UIGraphicsGetCurrentContext seems to return nil

查看:567
本文介绍了UIGraphicsGetCurrentContext似乎返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将单个PDF页面转换为PNG,并且它完美地运行直到UIGraphicsGetCurrentContext突然开始返回nil。

I'm trying to convert individual PDF pages into PNGs here, and it's worked perfectly until UIGraphicsGetCurrentContext suddenly started returning nil.

我正在尝试回溯我的步骤在这里,但我不太确定我知道这发生了什么。我的框架不是0,我看到可能会产生这个问题,但除此之外所有东西看起来都是正确的。

I'm trying to retrace my steps here, but I'm not quite sure that I know at which point this happened. My frame is not 0, which I see might create this problem, but other than that everything "looks" correct.

这是我的代码的开头。

_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);

任何人都知道还有什么可能导致零上下文?

Anybody have any idea what else might be causing the nil context?

推荐答案

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

1.添加

Indeed, it is possible to have CGContextRef object reusable after it has been set in drawRect method. The point is - you need to push the Context to the stack before using it from anywhere. Otherwise, current context will be 0x0
1. Add

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


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


2. In your @implementation first set renderFirst to TRUE before view has appeared on the screen, then:

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


3。在设置上下文后将某些内容呈现给上下文。


3. Rendering Something to the context after the context was set.

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



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


Here is an example exactly matching the thread case:

- (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();
}



从以前拍摄的上下文中提取图像在这篇文章中:


And Drawing image from context that was taken before in this post:

-(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内的直接方法消息调用。在performselection调用的情况下,Context不会向屏幕呈现任何结果。因此,我建议使用CALayer作为屏幕目标的渲染,而不是直接使用图形上下文。

希望它有所帮助。

Do not call UIGraphicsPopContext() function until you need the context to render your objects.
It seems that CGContextRef is being removed from the top of the graphic stack automatically when the calling method finishes.
Anyway, this example seems to be a kind of Hack - not planned and proposed by Apple. The solution is very unstable and works only with direct method messages calls inside only one UIView that is on the top of the screen. In case of "performselection" calls, Context does not render any results to the screen. So, I suggest to use CALayer as a rendering to the screen target instead of direct graphic context usage.
Hope it helps.

这篇关于UIGraphicsGetCurrentContext似乎返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆