UIGraphicsGetCurrentContext seems to return nil

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

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.

  • How to reload only data section of UICollectionView?
  • GMSMapView myLocation not giving actual location
  • UITextView: Disable selection, allow links
  • Blurry UILabel when added programmatically
  • Internal navigation rejected: <allow-navigation not set> in Cordova on iOS
  • CMVideoFormatDescriptionGetCleanAperture() swift error
  • Here’s the beginning of my code.

    _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?

    3 Solutions Collect From Internet About “UIGraphicsGetCurrentContext seems to return nil”

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

    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.

    It doesn’t have to be called from “drawRect”.
    you can also call it after “UIGraphicsBeginImageContext(bRect.size);”

    Check in following line

    UIGraphicsBeginImageContext(bRect.size);
    

    if bRect.size is not 0,0

    In my case, this was the reason why the returned context on the following line was null.

    Are you calling UIGraphicsGetCurrentContext() inside of the drawRect method? As far as I know, it can only be called within drawRect, otherwise it will just return nil.