我發現了一些代碼,它使我從PDF文件中獲得了UIImage
。它有效,但我有兩個問題:將PDF轉換爲UIImageView
- 有沒有可能實現更好的UIImage質量? (看截圖)
- 我只看到我的
UIImageView
的第一頁。我是否必須將該文件嵌入UIScrollView
才能完成? - 或者只渲染一個頁面並使用按鈕瀏覽頁面會更好嗎?
P.S.我知道UIWebView
可以顯示某些功能的PDF頁面,但我需要它作爲UIImage
或至少在UIView
。
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;
}