Ios 将PDF转换为UIImageView

Ios 将PDF转换为UIImageView,ios,xcode,image,pdf,Ios,Xcode,Image,Pdf,我从PDF文件中找到了一些代码,它给了我一个UIImage。这是可行的,但我有两个问题: 是否有可能获得更好的UIImage质量?(见截图) 我只能在我的UIImageView中看到第一页。我是否必须将文件嵌入UIScrollView中才能完成 还是只渲染一个页面并使用按钮在页面中导航更好 另外,我知道UIWebView可以显示带有某些功能的PDF页面,但我需要它作为UIImage或至少在UIView中显示 图像质量差: code: -(UIImage *)image { UIGraphi

我从PDF文件中找到了一些代码,它给了我一个
UIImage
。这是可行的,但我有两个问题:

  • 是否有可能获得更好的UIImage质量?(见截图)
  • 我只能在我的
    UIImageView
    中看到第一页。我是否必须将文件嵌入
    UIScrollView
    中才能完成
  • 还是只渲染一个页面并使用按钮在页面中导航更好
另外,我知道
UIWebView
可以显示带有某些功能的PDF页面,但我需要它作为
UIImage
或至少在
UIView
中显示

图像质量差:

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;
}

你用CGContextTranslateCm(上下文,0.0320)做什么呼叫

您应该从pdf中提取适当的指标,代码如下:

 cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
 rotate = CGPDFPageGetRotationAngle(page);
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);
此外,如您所见,pdf可能具有旋转信息,因此您需要根据角度使用
CGContextTranslateCTM/CGContextRotateCTM/CGContextScaleCTM

您可能还想剪辑
CropBox
区域之外的任何内容,因为pdf有各种
视口
,您通常不想显示这些视口(例如,对于打印机,以便可以进行无缝打印)->使用
CGContextClip


接下来,您忘记了pdf参考定义了白色背景色。有很多文档根本没有定义任何背景颜色-如果你不自己绘制白色背景,你会得到奇怪的结果-->
CGContextSetRGBFillColor
&
CGContextFillRect

我知道我来晚了一点,但我希望我能帮助其他人寻找答案。 关于提出的问题:

  • 恐怕获得更好图像质量的唯一方法是渲染更大的图像,并让
    UIImageView
    为您调整其大小。我不认为你可以设置分辨率,但使用更大的图像可能是一个不错的选择。页面渲染时间不会太长,图像质量也会更好。PDF文件根据缩放级别按需呈现,这就是为什么它们看起来“质量更好”

  • 至于呈现所有页面,您可以调用
    CGPDFDocumentGetNumberOfPages(pdf)
    获取文档中的页面数,并使用一个简单的
    for
    循环将生成的所有图像浓缩到一个图像中。要显示它,请使用
    UIScrollVIew

  • 在我看来,这种方法比上述方法更好,但您应该尝试对其进行优化,例如始终呈现当前、上一页和下一页。为了获得良好的滚动过渡效果,为什么不使用水平
    UIScrollView

对于更通用的渲染代码,我始终按如下方式执行旋转:

 cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
 rotate = CGPDFPageGetRotationAngle(page);
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);
Steipet已经提到设置白色背景:

CGContextSetRGBFillColor(context, 1, 1, 1, 1);
CGContextFillRect(context, CGRectMake(0, 0, imageSize.width, imageSize.height)); 
因此,最后要记住的是,在导出图像时,将质量设置为最大值。例如:

UIImageJPEGRepresentation(image, 1);