CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]'-Collection of common programming errors
Have a pdfscrollview
CGPDFPageRef PDFPage = CGPDFDocumentGetPage(thePDF, [_page intValue]);
pdfScrollView = [[PDFScrollView alloc] initWithFrame:CGRectMake(0, 45, self.view.bounds.size.width, 925)];
[pdfScrollView setPDFPage:PDFPage];
I added pdf path to open pdf file in pdf scrollview
NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"pdf"];
PageViewController *page = [[PageViewController alloc] initWithPDFAtPath:path];
[self presentViewController:page animated:NO completion:NULL];
[self.view addSubview:pdfScrollView];
[pdfScrollView addSubview:page];
but app crashes with the error
: CGContextSetRGBFillColor: invalid context 0x0
: CGContextFillRects: invalid context 0x0
: CGContextSaveGState: invalid context 0x0
: CGContextTranslateCTM: invalid context 0x0
: CGContextScaleCTM: invalid context 0x0
: CGContextScaleCTM: invalid context 0x0
: CGContextRestoreGState: invalid context 0x0
*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]'
This is the initWithPDFPath from pageviewcontroller class
-(id)initWithPDFAtPath:(NSString *)path {
NSURL *pdfUrl = [NSURL fileURLWithPath:path];
PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
totalPages = (int)CGPDFDocumentGetNumberOfPages(PDFDocument);
self = [super initWithNibName:nil bundle:nil];
return self;
}
How can i fix this error.
EDIT:
Added Exception breakpoint to debug and got this
[pdfScrollView setPDFPage:PDFPage];
and this is the method to setPDFPage
- (void)setPDFPage:(CGPDFPageRef)PDFPage;
{
CGPDFPageRetain(PDFPage);
CGPDFPageRelease(_PDFPage);
_PDFPage = PDFPage;
// Determine the size of the PDF page.
CGRect pageRect = CGPDFPageGetBoxRect(_PDFPage, kCGPDFMediaBox);
_PDFScale = self.frame.size.width/pageRect.size.width;
pageRect.size = CGSizeMake(pageRect.size.width*_PDFScale, pageRect.size.height*_PDFScale);
/*
Create a low resolution image representation of the PDF page to display before the TiledPDFView renders its content.
*/
UIGraphicsBeginImageContext(pageRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// First fill the background with white.
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,pageRect);
CGContextSaveGState(context);
// Flip the context so that the PDF page is rendered right side up.
CGContextTranslateCTM(context, 0.0, pageRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Scale the context so that the PDF page is rendered at the correct size for the zoom level.
CGContextScaleCTM(context, _PDFScale,_PDFScale);
CGContextDrawPDFPage(context, _PDFPage);
CGContextRestoreGState(context);
UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (self.backgroundImageView != nil) {
[self.backgroundImageView removeFromSuperview];
}
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
backgroundImageView.frame = pageRect;
backgroundImageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:backgroundImageView];
[self sendSubviewToBack:backgroundImageView];
self.backgroundImageView = backgroundImageView;
// Create the TiledPDFView based on the size of the PDF page and scale it to fit the view.
TiledPDFView *tiledPDFView = [[TiledPDFView alloc] initWithFrame:pageRect scale:_PDFScale];
[tiledPDFView setPage:_PDFPage];
[self addSubview:tiledPDFView];
self.tiledPDFView = tiledPDFView;
}
Thanks for help.