首页 文章

模糊PDF绘图与CATiledLayer相比

提问于
浏览
3

在我的应用程序中,我正在创建一个PDF阅读器 . 为了满足性能,我以默认比例绘制视图 . 此视图不是基于CATiledLayer的 . 当用户放大一点时,我用CATiledLayer覆盖这个视图,在正常使用情况下,它提供更清晰的图像和低内存使用 .

现在问题是在默认状态下(当显示CALayer视图时)图像出现有点模糊 . 当我尝试放大一点点并且CATiledLayer开始时,相同的文本和一切都变得尖锐 .

这是CATiledLayer视图的代码(显示清晰的图像和文本):

- (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);    
}

这是普通视图的代码(显示正常,但图像与CATiledLayer的质量不同):

- (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); 

}

1 回答

  • 2

    您是否尝试在执行缩放后设置视图的内容比例因子?

    在您的UIScrollViewDelegate中,您将执行以下操作:

    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
    {
        [view setContentScaleFactor:scale];
    }
    

相关问题