Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIGraphicsGetCurrentContext seems to return nil

Tags:

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.

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?

like image 829
Christian A. Strømmen Avatar asked Sep 14 '12 12:09

Christian A. Strømmen


2 Answers

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.

like image 189
Matt Avatar answered Oct 04 '22 19:10

Matt


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.

like image 42
docksteaderluke Avatar answered Oct 04 '22 19:10

docksteaderluke