与CATiledLayer相比,模糊的PDF绘图问题

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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接