2011-10-30 24 views
8

我已經使用以下方法從UIView生成PDF。它們都創建PDF,但失去的質量:iOS - 通過渲染丟失質量從UIView生成PDF

方法1:

@implementation UIView(PDFWritingAdditions) 

- (void)renderInPDFFile:(NSString*)path 
{ 
    CGRect mediaBox = self.bounds; 
    CGContextRef ctx = CGPDFContextCreateWithURL((__bridge_retained CFURLRef)[NSURL fileURLWithPath:path], &mediaBox, NULL); 
    CGPDFPageRef page; 
    CGContextDrawPDFPage(ctx, page); 

    CGPDFContextBeginPage(ctx, NULL); 

    // Also tried following commented lines but no luck 
    // CGContextSetFlatness(ctx, 0.1); 
    // CGContextSetAllowsAntialiasing(ctx, YES); 
    // CGContextSetAllowsFontSmoothing(ctx, YES); 
    // CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); 
    CGContextScaleCTM(ctx, 1, -1); 
    CGContextTranslateCTM(ctx, 0, -mediaBox.size.height); 
    [self.layer renderInContext:ctx]; 
    CGPDFContextEndPage(ctx); 
    CFRelease(ctx); 
} 

@end 

方法2:

- (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename 
{ 
    // Creates a mutable data object for updating with binary data, like a byte array 
    NSMutableData *pdfData = [NSMutableData data]; 

    // Points the pdf converter to the mutable data object and to the UIView to be converted 
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil); 
    UIGraphicsBeginPDFPage(); 
    CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData 

    [aView.layer renderInContext:pdfContext]; 

    // remove PDF rendering context 
    UIGraphicsEndPDFContext(); 

    // Retrieves the document directories from the iOS device 
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 

    NSString* documentDirectory = [documentDirectories objectAtIndex:0]; 
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; 

    // instructs the mutable data object to write its context to a file on disk 
    [pdfData writeToFile:documentDirectoryFilename atomically:YES]; 
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); 
} 

我想我可能會丟失的PDP上下文的一些設置,不知道。此外,我已經從UIView的創建PNG圖片下面的方法是完全一樣的品質:

- (UIImage *)imageFromView:(UIView *)view 
{ 
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 
    // Retrieves the document directories from the iOS device 
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 

    NSString* documentDirectory = [documentDirectories objectAtIndex:0]; 
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"pdf.png"]; 

    // instructs the mutable data object to write its context to a file on disk 
    [UIImagePNGRepresentation(img) writeToFile:documentDirectoryFilename atomically:YES]; 
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); 
    return img; 
} 

有人可以看到我在做什麼錯? 謝謝。

+0

它是如何失去質量? –

+0

以下是原始視圖鏈接:和pdf鏈接: iEamin

+0

可能的重複[高質量UIImage從PDF](http://stackoverflow.com/questions/14152477/high-quality-uiimage-from-pdf) – nielsbot

回答

2

這是使用視網膜顯示器嗎?它可能是一個contentsScale的東西。看看this SO post

編輯 好了,現在你已經發布的前&圖片後,我看到你想要的發票的UIView被渲染的那樣好,質量高,打印矢量數據在PDF 。

我非常確定[CALayer renderInContext]總是生成柵格數據,因此您可能更適合手動構建PDF。如果您正在談論稅務發票,您是否真的希望佈局隨着下一次iOS更新而改變?

但是,您可能look at this answer,它不情願地建議使用UIView作爲您的PDF佈局的模板系統。

+0

不,這發生在正常顯示。 – iEamin

+0

我已經爲pdf創建了手動視圖[因爲尚未找到解決方案],並使用UIView獲取點和顏色作爲參考[以便如果我更改視圖中的任何內容,我不必更改代碼]。但我添加的原始圖像鏈接是使用「imageFromView」方法生成的,也是一個光柵圖像。所以,我的觀點是「renderInContext」應該能夠生成相同質量的pdf。此外,我試圖從該圖像創建PDF,但沒有運氣。 。 。同樣的問題。 – iEamin

+0

您需要從調用CGContextFillRect,CTFontDrawGlyphs等創建PDF。renderInContext將始終創建看起來糟糕的屏幕分辨率柵格PDF。 –

0

看着您的示例圖像,我不確定您的期望是否正確...您的預覽窗口部分放大,而您正在將PDF圖像繪製爲位圖圖像 - 因此您會看到「鋸齒狀邊緣'。如果您查看實際大小(從預覽或Cmd-0的視圖菜單中查看),則應該在屏幕截圖中看到或多或少相同的圖像。

但是,您真正想做的不僅僅是將您的位圖UIView渲染到PDF上下文中,而是將CoreGraphics繪圖和佈局直接轉換爲PDF上下文。查看本教程http://www.raywenderlich.com/6818/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-2作爲示例。這將確保您的文本和線條在放大時保持清晰,並確保在紙張上打印優質的產品。 Quartz2D矢量圖形是你的朋友在這裏,因爲你永遠不會有位高品質的,至少在沒有創建一個龐大的PDF文件...

0

編輯好吧,它不是完全一樣的問題,但我認爲解決方案將類似於

我覺得這是High quality UIImage from PDF

愚弄的人見我的回答有:

https://stackoverflow.com/a/14152516/210171

當您詢問PDF的頁面大小時,您得到72 PPI的寬度/高度 。您可以嘗試創建使用 比例變換放大的上下文。例如,如果要以300 dpi的分辨率進行渲染,請將 的縮放變換按比例縮小300.0/72.0

如果您導出爲TIFF,您將能夠封裝生成圖像的最終PPI (300)。