2011-12-13 144 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);