【发布时间】:2012-01-19 09:17:02
【问题描述】:
我找到了一些代码,它从 PDF 文件中给了我一个UIImage。它有效,但我有两个问题:
- 是否有可能获得更好的 UIImage 质量? (见截图)
- 我只看到
UIImageView的第一页。我是否必须将文件嵌入到UIScrollView中才能完成? - 还是只呈现一个页面并使用按钮浏览页面更好?
附:我知道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;
}
【问题讨论】: