2012-09-26 66 views
0

我有以下代码,呈现与页面中心的图像的PDF。然而,图像(实际上也是文本)看起来非常像素化,显然不是为视网膜显示设计的。视网膜PDF图像渲染为iOS

我在下面的代码中没有做什么?

-(void)drawText 
{ 
    NSString* fileName = @"Workbook.PDF"; 

    NSArray *arrayPaths = 
    NSSearchPathForDirectoriesInDomains(
            NSDocumentDirectory, 
            NSUserDomainMask, 
            YES); 
    NSString *path = [arrayPaths objectAtIndex:0]; 
    NSString* pdfFileName = [path stringByAppendingPathComponent:fileName]; 

    // Create the PDF context using the default page size of 612 x 792. 
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil); 
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES); 

    for(UIImage *image in _pages) 
    { 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSString* textToDraw = [NSString stringWithFormat:@"Page %i of %@", [_pages indexOfObject:image] + 1, [defaults objectForKey:@"OpenBookKey"]]; 
    CFStringRef stringRef = (__bridge CFStringRef)textToDraw; 

    // Prepare the text using a Core Text Framesetter. 
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL); 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 

    CGRect frameRect = CGRectMake(230, 0, 612, 50); 
    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); 

    // Mark the beginning of a new page. 
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil); 

    // Get the graphics context. 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    CGRect frame = CGRectMake(20, 50, 550, 550); 
    [image drawInRect:frame]; 

    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 

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

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

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

    } 

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

} 

回答

0

与Retina显示屏无关,您只需调整图像大小以减少像素化。你可以参考我的代码,你需要添加代码来管理你的图片大小,调整它的大小

UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil); 
for (UIImage *img in FileArr) { 
    CGRect imageFrame = CGRectMake(0,0, img.size.width, img.size.height); 
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, img.size.width, img.size.height), nil); 
    [img drawInRect:imageFrame]; 
    NSString *pageString = [NSString stringWithFormat:@"Page 122"]; 
    UIFont *theFont = [UIFont systemFontOfSize:12]; 
    CGSize maxSize = CGSizeMake(612, 72); 
    CGSize pageStringSize = [pageString sizeWithFont:theFont 
            constrainedToSize:maxSize 
             lineBreakMode:UILineBreakModeClip]; 
    CGRect stringRect = CGRectMake(((612.0 - pageStringSize.width)/2.0), 
            720.0 + ((72.0 - pageStringSize.height)/2.0), 
            pageStringSize.width, 
            pageStringSize.height); 
    [pageString drawInRect:stringRect withFont:theFont]; 
} 
UIGraphicsEndPDFContext(); 
0

PDF上下文与Retina显示无关。该图像可能会出现像素化,因为它是以小分辨率绘制的,并且文本显示为像素化,因为它被绘制到中间位图上,然后在PDF页面上绘制位图(这似乎是内部工作的方式,SO上的其他用户抱怨像素化文字)。
图像问题可以通过使用高分辨率图像来解决,文本问题我不知道。