我正在使用具有 Retina Display 2048x1536 分辨率(2x 比例)的全屏 iPad 从 UIViews 创建 PDF 文件。生成的文件非常大,我得到 6-10 mb 的文件,用于 2 页相当简单的视图。理想情况下,每个文档我会达到 4-5 页,但当前的方法会使 PDF 尺寸过大。
有没有办法使以这种方式创建的 PDF 更小?就像创建视图的屏幕截图,将其转换为 JPG,然后将其写入 PDF 上下文?还是我缺少一些质量选项?
这是从单个视图创建 PDF 的代码:
+(NSMutableData *)createPDFDatafromUIView:(UIView*)aView
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
return pdfData;
}
+(NSString*)createPDFfromUIView:(UIView*)aView saveToFilepath:(NSString*)filepath
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [AppGraphics createPDFDatafromUIView:aView];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:filepath atomically:YES];
DLog(@"saving PDF to: %@",filepath);
DLog(@"file exists: %@",[[NSFileManager defaultManager] fileExistsAtPath:filepath]?@"YES":@"NO");
return filepath;
}
这是我用来将多个 PDF 连接在一起的方法
+ (NSString *)joinPDF:(NSArray *)listOfPaths saveToPath:(NSString*)path
{
CFURLRef pdfURLOutput = ( CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:path]);
NSInteger numberOfPages = 0;
// Create the output context
CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);
for (NSString *source in listOfPaths) {
CFURLRef pdfURL = ( CFURLRef)CFBridgingRetain([[NSURL alloc] initFileURLWithPath:source]);
//file ref
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);
numberOfPages = CGPDFDocumentGetNumberOfPages(pdfRef);
// Loop variables
CGPDFPageRef page;
CGRect mediaBox;
// Read the first PDF and generate the output pages
DLog(@"GENERATING PAGES FROM PDF 1 (%@)...", source);
for (int i=1; i<=numberOfPages; i++) {
page = CGPDFDocumentGetPage(pdfRef, i);
mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGContextBeginPage(writeContext, &mediaBox);
CGContextDrawPDFPage(writeContext, page);
CGContextEndPage(writeContext);
}
CGPDFDocumentRelease(pdfRef);
CFRelease(pdfURL);
}
CFRelease(pdfURLOutput);
// Finalize the output file
CGPDFContextClose(writeContext);
CGContextRelease(writeContext);
return path;
}