{"id":1968,"date":"2022-08-30T15:20:57","date_gmt":"2022-08-30T15:20:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/06\/calayerinvalidgeometry-reason-calayer-position-contains-nan-nan-nan-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:57","modified_gmt":"2022-08-30T15:20:57","slug":"calayerinvalidgeometry-reason-calayer-position-contains-nan-nan-nan-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/calayerinvalidgeometry-reason-calayer-position-contains-nan-nan-nan-collection-of-common-programming-errors\/","title":{"rendered":"CALayerInvalidGeometry&#39;, reason: &#39;CALayer position contains NaN: [nan nan]&#39;-Collection of common programming errors"},"content":{"rendered":"<p>Have a pdfscrollview<\/p>\n<pre><code> CGPDFPageRef PDFPage = CGPDFDocumentGetPage(thePDF, [_page intValue]);\n\npdfScrollView = [[PDFScrollView alloc] initWithFrame:CGRectMake(0, 45, self.view.bounds.size.width, 925)];\n\n[pdfScrollView setPDFPage:PDFPage];\n<\/code><\/pre>\n<p>I added pdf path to open pdf file in pdf scrollview<\/p>\n<pre><code>NSString *path = [[NSBundle mainBundle] pathForResource:@\"filename\" ofType:@\"pdf\"];\n\nPageViewController *page = [[PageViewController alloc] initWithPDFAtPath:path];\n\n[self presentViewController:page animated:NO completion:NULL];\n\n [self.view addSubview:pdfScrollView];\n\n[pdfScrollView addSubview:page];\n<\/code><\/pre>\n<p>but app crashes with the error<\/p>\n<pre><code>: CGContextSetRGBFillColor: invalid context 0x0\n: CGContextFillRects: invalid context 0x0\n: CGContextSaveGState: invalid context 0x0\n: CGContextTranslateCTM: invalid context 0x0\n: CGContextScaleCTM: invalid context 0x0\n: CGContextScaleCTM: invalid context 0x0\n: CGContextRestoreGState: invalid context 0x0\n*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]'\n<\/code><\/pre>\n<p>This is the initWithPDFPath from pageviewcontroller class<\/p>\n<pre><code>-(id)initWithPDFAtPath:(NSString *)path {\n\nNSURL *pdfUrl = [NSURL fileURLWithPath:path];\n\nPDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);\n\ntotalPages = (int)CGPDFDocumentGetNumberOfPages(PDFDocument);\n\nself = [super initWithNibName:nil bundle:nil];\n\nreturn self;\n\n }\n<\/code><\/pre>\n<p>How can i fix this error.<\/p>\n<p>EDIT:<\/p>\n<p>Added Exception breakpoint to debug and got this<\/p>\n<pre><code>[pdfScrollView setPDFPage:PDFPage];\n<\/code><\/pre>\n<p>and this is the method to setPDFPage<\/p>\n<pre><code>  - (void)setPDFPage:(CGPDFPageRef)PDFPage;\n {\nCGPDFPageRetain(PDFPage);\nCGPDFPageRelease(_PDFPage);\n_PDFPage = PDFPage;\n\n\/\/ Determine the size of the PDF page.\nCGRect pageRect = CGPDFPageGetBoxRect(_PDFPage, kCGPDFMediaBox);\n_PDFScale = self.frame.size.width\/pageRect.size.width;\npageRect.size = CGSizeMake(pageRect.size.width*_PDFScale, pageRect.size.height*_PDFScale);\n\n\n\/*\n Create a low resolution image representation of the PDF page to display before the TiledPDFView renders its content.\n *\/\nUIGraphicsBeginImageContext(pageRect.size);\n\nCGContextRef context = UIGraphicsGetCurrentContext();\n\n\/\/ First fill the background with white.\nCGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);\nCGContextFillRect(context,pageRect);\n\nCGContextSaveGState(context);\n\/\/ Flip the context so that the PDF page is rendered right side up.\nCGContextTranslateCTM(context, 0.0, pageRect.size.height);\nCGContextScaleCTM(context, 1.0, -1.0);\n\n\/\/ Scale the context so that the PDF page is rendered at the correct size for the zoom level.\nCGContextScaleCTM(context, _PDFScale,_PDFScale);    \nCGContextDrawPDFPage(context, _PDFPage);\nCGContextRestoreGState(context);\n\nUIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();\n\nUIGraphicsEndImageContext();\n\n\nif (self.backgroundImageView != nil) {\n    [self.backgroundImageView removeFromSuperview];\n}\n\nUIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];\nbackgroundImageView.frame = pageRect;\nbackgroundImageView.contentMode = UIViewContentModeScaleAspectFit;\n[self addSubview:backgroundImageView];\n[self sendSubviewToBack:backgroundImageView];\nself.backgroundImageView = backgroundImageView;\n\n\/\/ Create the TiledPDFView based on the size of the PDF page and scale it to fit the view.\nTiledPDFView *tiledPDFView = [[TiledPDFView alloc] initWithFrame:pageRect scale:_PDFScale];\n[tiledPDFView setPage:_PDFPage];\n\n[self addSubview:tiledPDFView];\nself.tiledPDFView = tiledPDFView;\n }\n<\/code><\/pre>\n<p>Thanks for help.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-12-06 06:25:52. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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:@&#8221;filename&#8221; ofType:@&#8221;pdf&#8221;]; PageViewController *page = [[PageViewController alloc] initWithPDFAtPath:path]; [self presentViewController:page animated:NO completion:NULL]; [self.view addSubview:pdfScrollView]; [pdfScrollView addSubview:page]; but app crashes with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1968","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1968"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1968\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}