我发现了一些代码,它使我从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;
}