objective-cpdfcatiledlayercgpdfdocumentref

Correct way of adding contents to pdf on zooming ios


I am working in an app which displays map in pdf. In that I am having the basic map pdf (pdf1) on top of which I need to show another pdf (pdf2) layer which is having cities name.

I am able to do so by adding another CATiledLayer object on the view like below:

 NSString *filePath = [[NSBundle mainBundle]  pathForResource:fileName ofType:@"pdf"];
NSURL *pdfURL = [NSURL fileURLWithPath:filePath];
CGPDFDocumentRef myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);
_PDFPageRef =  CGPDFDocumentGetPage(myDocumentRef, 1);


CATiledLayer *tiledLayer = [CATiledLayer layer];
tiledLayer.delegate = self;
tiledLayer.levelsOfDetail = 3; // Zoom levels

tiledLayer.levelsOfDetailBias = 3; // Bias
tiledLayer.backgroundColor = [UIColor clearColor].CGColor;
UIScreen *mainScreen = [UIScreen mainScreen]; // Main screen

CGFloat screenScale = [mainScreen scale]; // Main screen scale

CGRect screenBounds = [mainScreen bounds]; // Main screen bounds

CGFloat w_pixels = (screenBounds.size.width * screenScale);

CGFloat h_pixels = (screenBounds.size.height * screenScale);

CGFloat max = ((w_pixels < h_pixels) ? h_pixels : w_pixels);

CGFloat sizeOfTiles = ((max < 512.0f) ? 512.0f : 1024.0f);

tiledLayer.tileSize = CGSizeMake(sizeOfTiles, sizeOfTiles);
tiledLayer.frame =  CGRectIntegral(CGPDFPageGetBoxRect(_PDFPageRef,  kCGPDFCropBox));
[[self layer] addSublayer:tiledLayer];
[self setNeedsDisplay];

But I am facing two problems here:

  1. If pdf1 is not loaded completely and I add another pdf2 on top of it then pdf1 will never be loaded completely. It will be blurred.

  2. If pdf1 is loaded completely, then on adding pdf2 and zooming after zooming 3,4 times, pdf1 becomes blurr.

Map with pdf1 Map with pdf2 added on pdf1

Please help!!


Solution

  • I have got the solution for adding more contents to pdf on zooming. As per my understanding, what I was doing wrong was I was adding another CATiledLayer (tiledLayer) object on the same view and the view was considering this object as the present tile and view was working on this tile (tiledLayer). That is why the faded view of the previous tile was not getting clear.

    Now I am using the same tile for adding more details on top of it. I used another CGPDFPageRef object (_PDFPageRef2)

    CGPDFDocumentRef myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);
    
    _PDFPageRef2 =  CGPDFDocumentGetPage(myDocumentRef, 1);
    

    After this I am calling setNeedsDisplay method on the view

    [self setNeedsDisplay];
    

    This method draws the view again and thus call -drawRect: method of the view whose implemntation is as below:

    -(void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext();

    float dpi = 100.0 / 72.0;
    
    ReaderContentPage *readerContentPage = self; // Retain self
    
    //  CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // White
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, CGContextGetClipBoundingBox(context)); // Fill
    
    //NSLog(@"%s %@", __FUNCTION__, NSStringFromCGRect(CGContextGetClipBoundingBox(context)));
    
    CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); CGContextScaleCTM(context, 1.0f, -1.0f);
    
    
    CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(_PDFPageRef, kCGPDFCropBox, self.bounds, 0, true));
    
    //CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); CGContextSetInterpolationQuality(context, kCGInterpolationDefault);
    
    CGContextDrawPDFPage(context, _PDFPageRef); // Render the PDF1 page into the context
    if (_PDFPageRef2) {
        CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(_PDFPageRef2, kCGPDFCropBox, self.bounds, 0, true));
    
        //CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); CGContextSetInterpolationQuality(context, kCGInterpolationDefault);
    
        CGContextDrawPDFPage(context, _PDFPageRef2); // Render the PDF2 page into the context
    
    }
    if (readerContentPage != nil) readerContentPage = nil; // Release self
                                                           //    UIGraphicsPushContext(context);
                                                           //    CGSize size = CGSizeMake(320, 480); //Screen Size on iPhone device
                                                           //    UIGraphicsBeginImageContext(size);  //Create a new Bitmap-based graphics context (also makes this the current context)
    

    }

    Thus, I am able to add more details to pdf on zooming.

    enter image description here