模糊PDF绘图与CATiledLayer相比

时间:2011-11-11 13:27:57

标签: iphone objective-c ios pdf core-graphics

在我的应用程序中,我正在创建一个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 个答案:

答案 0 :(得分:2)

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

在您的UIScrollViewDelegate中,您可以:

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