2011-12-13 141 views
2

我發現了一些代碼,它使我從PDF文件中獲得了UIImage。它有效,但我有兩個問題:將PDF轉換爲UIImageView

  • 有沒有可能實現更好的UIImage質量? (看截圖)
  • 我只看到我的UIImageView的第一頁。我是否必須將該文件嵌入UIScrollView才能完成?
  • 或者只渲染一個頁面並使用按鈕瀏覽頁面會更好嗎?

P.S.我知道UIWebView可以顯示某些功能的PDF頁面,但我需要它作爲UIImage或至少在UIView

Bad quality Image:

Bad quality Image

Code:

-(UIImage *)image { 
UIGraphicsBeginImageContext(CGSizeMake(280, 320)); 

CGContextRef context = UIGraphicsGetCurrentContext(); 

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("ls.pdf"), NULL, NULL); 

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); 

CGContextTranslateCTM(context, 0.0, 320); 

CGContextScaleCTM(context, 1.0, -1.0); 

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 4); 

CGContextSaveGState(context); 

CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, 280, 320), 0, true); 

CGContextConcatCTM(context, pdfTransform); 

CGContextDrawPDFPage(context, page); 

CGContextRestoreGState(context); 

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return resultingImage; 
} 

回答

1

你在與CGContextTranslateCTM(context, 0.0, 320);電話幹什麼?

你應該提取適當的指標形成的PDF,用這樣的代碼:

cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox); 
rotate = CGPDFPageGetRotationAngle(page); 

而且,正如你看到的,PDF威力有旋轉的信息,所以你需要使用取決於角度CGContextTranslateCTM/CGContextRotateCTM/CGContextScaleCTM

您也可能想剪輯是CropBox區域之外的任何內容,如PDF有各種viewPorts你平時不想顯示(例如,用於打印機,這樣完美的打印是可能的) - >使用CGContextClip

接下來,您忘記了PDF參考定義了白色背景顏色。有很多文件沒有定義任何背景顏色 - 如果你自己沒有繪製白色背景,你會得到奇怪的結果 - >CGContextSetRGBFillColor & CGContextFillRect

2

我知道我在這裏有點晚,但我希望我可以幫助別人尋找答案。 至於問的問題:

  • 恐怕要達到更好的圖像質量的唯一方法是使一個更大的圖像,並讓UIImageView調整它。我不認爲你可以設置分辨率,但使用更大的圖像可能是一個不錯的選擇。頁面渲染不會太長,圖像質量會更好。 PDF文件根據需要根據縮放級別進行渲染,這就是爲什麼它們具有「更好的質量」。

  • 至於渲染所有頁面,您可以獲得調用CGPDFDocumentGetNumberOfPages(pdf)的文檔中的頁面數,並使用簡單的for循環可以將所有在單個圖像中生成的圖像連接起來。爲了顯示它,請使用UIScrollVIew

  • 在我看來,這種方法比上面的更好,但你應該嘗試進行優化,例如渲染總是當前,前一個和下一個頁面。爲了更好的滾動過渡效果,爲什麼不使用水平的UIScrollView

更多通用渲染代碼,我總是做旋轉這樣的:

int rotation = CGPDFPageGetRotationAngle(page); 

CGContextTranslateCTM(context, 0, imageSize.height);//moves up Height 
CGContextScaleCTM(context, 1.0, -1.0);//flips horizontally down 
CGContextRotateCTM(context, -rotation*M_PI/180);//rotates the pdf 
CGRect placement = CGContextGetClipBoundingBox(context);//get the flip's placement 
CGContextTranslateCTM(context, placement.origin.x, placement.origin.y);//moves the the correct place 

//do all your drawings 
CGContextDrawPDFPage(context, page); 

//undo the rotations/scaling/translations 
CGContextTranslateCTM(context, -placement.origin.x, -placement.origin.y); 
CGContextRotateCTM(context, rotation*M_PI/180); 
CGContextScaleCTM(context, 1.0, -1.0); 
CGContextTranslateCTM(context, 0, -imageSize.height); 

Steipete已經提到設置白色背景:

CGContextSetRGBFillColor(context, 1, 1, 1, 1); 
CGContextFillRect(context, CGRectMake(0, 0, imageSize.width, imageSize.height)); 

所以最後要記住的介意在導出圖像時,將質量設置爲最大值。例如:

UIImageJPEGRepresentation(image, 1);