【问题标题】:Blurry PDF Drawing compared to CATiledLayer与 CATiledLayer 相比,PDF 绘图模糊
【发布时间】:2011-12-27 00:50:57
【问题描述】:

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

}

【问题讨论】:

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


    【解决方案1】:

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

    在你的 UIScrollViewDelegate 中你会这样做:

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

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多