Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blurry PDF Drawing compared to CATiledLayer

In my application I'm creating a PDF reader. To cater for performance, I draw a view at default scale. This view is not CATiledLayer based. When user zooms in a bit, I overlay this view with a CATiledLayer one which gives much crisper image and low memory usage in normal use cases.

Now the problem is that in default state (when CALayer view is being shown) the image is coming out a bit blurry. When I try to zoom in just a wee bit and CATiledLayer kicks in, the same text and everything becomes sharp.

Here's the code for the CATiledLayer view (which shows crisp image and text as it should be):

- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)context{

    CGPDFPageRef drawPDFPageRef = NULL;
    CGPDFDocumentRef drawPDFDocRef = NULL;  

    @synchronized(self) {
        if (_PDFDocRef==nil) {
            NSLog(@"PDF NIL");
            _PDFDocRef = [SharedPDFDocRef sharedInstanceForUrl:_fileURL andPhrase:_password];
            _PDFPageRef = CGPDFDocumentGetPage(_PDFDocRef, _page);
        }
        drawPDFDocRef = _PDFDocRef;
        drawPDFPageRef = _PDFPageRef;
    }   

    if (drawPDFPageRef != NULL) {
        CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); 
        CGContextScaleCTM(context, 1.0f, -1.0f);

        CGAffineTransform transform = 
        CGPDFPageGetDrawingTransform(drawPDFPageRef, kCGPDFCropBox, layer.bounds, 0, true);

        CGContextConcatCTM(context, transform);

        CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); 
        CGContextSetInterpolationQuality(context, kCGInterpolationDefault);

        CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); 
        CGContextFillRect(context, CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFBleedBox));

        CGContextDrawPDFPage(context, drawPDFPageRef); 
    }

    //CGPDFPageRelease(drawPDFPageRef); CGPDFDocumentRelease(drawPDFDocRef);    
}

Here's the code for the Normal view (shows okay but image is not in the same quality as CATiledLayer one):

- (void) drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGPDFPageRef drawPDFPageRef = NULL;
    CGPDFDocumentRef drawPDFDocRef = NULL;  

    @synchronized(self) {
        drawPDFDocRef = CGPDFDocumentRetain(_PDFDocRef);
        drawPDFPageRef = CGPDFPageRetain(_PDFPageRef);
    }
    // get the page reference
    CGPDFPageRef page = drawPDFPageRef;
    CGContextRef context = UIGraphicsGetCurrentContext();

    // white background

    // transformation/-lation
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.00f, -1.00f);
    CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page,
                                                             kCGPDFCropBox, self.bounds, 0, true));

    CGContextSetInterpolationQuality(context, kCGInterpolationDefault); 
    CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);


    CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
    CGContextFillRect(context, CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFBleedBox));

    CGContextDrawPDFPage(context, page);
    CGPDFPageRelease(drawPDFPageRef); 
    CGPDFDocumentRelease(drawPDFDocRef); 

}
like image 970
zakishaheen Avatar asked Dec 22 '25 02:12

zakishaheen


1 Answers

Have you tried to set the content scale factor of the view after the zoom was performed?

In your UIScrollViewDelegate you would do:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    [view setContentScaleFactor:scale];
}
like image 175
jverrijt Avatar answered Dec 24 '25 17:12

jverrijt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!