-(void) createJPGsFromPDF:(NSString *)fromPDFName
{
if (fromPDFName == nil || [fromPDFName isEqualToString:@""]) {
return;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *docPath = [documentsDir stringByAppendingPathComponent:fromPDFName];
NSURL *fromPDFURL = [NSURL fileURLWithPath:docPath];
CGPDFDocumentRef fromPDFDoc = CGPDFDocumentCreateWithURL((CFURLRef) fromPDFURL);
// Get Total Pages
unsigned long pages = CGPDFDocumentGetNumberOfPages(fromPDFDoc);
// Create Folder for store under "Documents/"
NSError *error = nil;
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *folderPath = [documentsDir stringByAppendingPathComponent:[fromPDFName stringByDeletingPathExtension]];
[fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
int i = 1;
for (i = 1; i <= pages; i++) {
CGPDFPageRef pageRef = CGPDFDocumentGetPage(fromPDFDoc, i);
CGPDFPageRetain(pageRef);
// determine the size of the PDF page
CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
// renders its content.
UIGraphicsBeginImageContext(pageRect.size);
CGContextRef imgContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(imgContext);
CGContextTranslateCTM(imgContext, 0, pageRect.size.height);
CGContextScaleCTM(imgContext, 1, -1);
// 调图片质量
CGContextSetInterpolationQuality(imgContext, kCGInterpolationHigh);
// 渲染意图
CGContextSetRenderingIntent(imgContext, kCGRenderingIntentDefault);
// 将pdf画道image上
CGContextDrawPDFPage(imgContext, pageRef);
CGContextRestoreGState(imgContext);
//PDF Page to image
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Release current source page
CGPDFPageRelease(pageRef);
// Store IMG
NSString *imgname = [NSString stringWithFormat:@"fromPDFName_%d.jpg", i];
NSString *imgPath = [folderPath stringByAppendingPathComponent:imgname];
// UIImageJPEGRepresentation(tempImage, 0.1) 图片转为NSData并压缩大小
[UIImageJPEGRepresentation(tempImage, 0.1) writeToFile:imgPath atomically:YES];
}
CGPDFDocumentRelease(fromPDFDoc);
}
Quartz2d--pdf转图片
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 网络里有各种高大上的报告,但是不好找到PDF或PPT版本,收集了很多JPG图片,又不适合分发,这个时候就需要用到J...
- 问题描述 用浏览器打开正常,但使用工具 wkhtmltopdf 转换成 PDF 文档后却看不到任何内容。 html...