2013-03-07 141 views
2

有什麼方法可以在支持視網膜顯示的ios上生成高分辨率PDF?我製作了一個基本的PDF生成器,但在我的iPad 3上它仍然看起來像素化。感謝您提供各種建議!在ios上生成高分辨率pdf

更新:

在我的PDF文件中的文本是光滑美觀。但是當我使用代碼來繪製這個pdf時,我在視網膜顯示上得到了像素化(繪圖代碼在底部)。

PDF生成:

NSString *fileName = [self.bookManager cacheBookFileForPage:i]; 
    UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil); 

    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 

    CGContextTranslateCTM(currentContext, 0, 100); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 

    // TODO: Temporary numbers 
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 768 * 2, 1024 * 2), nil); 


    NSString *textToDraw = @"text"; 
    CFStringRef stringRef = (__bridge CFStringRef)textToDraw; 

    CTFontRef ctFont= CTFontCreateWithName(CFSTR("Arial"), 20 * 2, &CGAffineTransformIdentity); 

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
           (__bridge id)ctFont, (NSString *)kCTFontAttributeName, 
           nil]; 

    NSAttributedString *string = [[NSAttributedString alloc] initWithString:textToDraw 
                   attributes:attributes]; 

    CFRelease(ctFont); 


    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)string); 

    // TODO: temporary   
    CGRect frameRect = CGRectMake(100, 100, 800 * 2, 50 * 2); 
    CGMutablePathRef framePath = CGPathCreateMutable(); 
    CGPathAddRect(framePath, NULL, frameRect); 

    // Get the frame that will do the rendering. 
    CFRange currentRange = CFRangeMake(0, 0); 
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
    CGPathRelease(framePath); 

    // Draw the frame. 
    CTFrameDraw(frameRef, currentContext); 

    CFRelease(frameRef); 
    CFRelease(stringRef); 
    CFRelease(framesetter); 

    // Close the PDF context and write the contents out. 
    UIGraphicsEndPDFContext(); 

PDF圖紙:

// This will return pageRef from PDF 
CGPDFPageRef page = [self.bookManager contentForPage:index + 1]; 

CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); 
CGContextFillRect(context, CGContextGetClipBoundingBox(context)); 
CGContextTranslateCTM(context, 0.0f, [self.view.layer bounds].size.height); 

CGRect transformRect = CGRectMake(0, 0, [self.view.layer bounds].size.width, [self.view.layer bounds].size.height); 
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, transformRect, 0, true); 

// And apply the transform. 
CGContextConcatCTM(context, pdfTransform); 

CGContextScaleCTM(context, 1.0f, -1.0f); 

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

CGContextDrawPDFPage(context, page); 
+1

你在文檔中有什麼樣的內容?如果你的圖像包含鋸齒,那麼你需要提高他們的分辨率。如果你的字體是像素化的,那麼你可能沒有使用基於矢量的字體 - 你使用的是什麼字體?你是如何生成PDF的? – halfer 2013-03-07 23:35:58

+0

@halfer你是對的。這個問題需要一些代碼:) – 2013-03-07 23:56:22

+0

一個更好的問題。好的,如果您在桌面計算機上查看生成的PDF,是否以高縮放級別顯示鋸齒形字體?如果沒有,那麼問題出現在您的顯示代碼中。 (不是iOS程序員,但現在你的問題中有代碼,有人可以幫助你)。 – halfer 2013-03-08 00:01:03

回答

0

我想你有一個問題是,你正在擴大你是畫什麼;不要使用這種轉換!只需創建您的pdf上下文並在其中進行本地繪製。你的字體看起來很脆,因爲它們現在是以全分辨率繪製的,而不是半分辨率,然後該字體的位圖被放大,這當然會引入鋸齒。

tl; dr:直接繪製pdf,不要對文字使用縮放變換。