Jor*_*ans 7 pdf uikit quartz-2d ios
我想在iOS 6应用程序中生成一个好看的PDF.
我试过了:
这是一个代码示例:
-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(NSString *) path
{
CGContextRef myOutContext = NULL;
NSURL * url;
url = [NSURL fileURLWithPath:path];
if (url != NULL) {
myOutContext = CGPDFContextCreateWithURL ((__bridge CFURLRef) url,
&inMediaBox,
NULL);
}
return myOutContext;
}
-(void)savePdf:(NSString *)outputPath
{
if (!pageViews.count)
return;
UIView * first = [pageViews objectAtIndex:0];
CGContextRef pdfContext = [self createPDFContext:CGRectMake(0, 0, first.frame.size.width, first.frame.size.height) path:outputPath];
for(UIView * v in pageViews)
{
CGContextBeginPage (pdfContext,nil);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(0, (int)(v.frame.size.height));
transform = CGAffineTransformScale(transform, 1, -1);
CGContextConcatCTM(pdfContext, transform);
CGContextSetFillColorWithColor(pdfContext, [UIColor whiteColor].CGColor);
CGContextFillRect(pdfContext, v.frame);
[v.layer renderInContext:pdfContext];
CGContextEndPage (pdfContext);
}
CGContextRelease (pdfContext);
}
Run Code Online (Sandbox Code Playgroud)
渲染的UIViews只包含一个UIImageView +一堆UILabel(一些带有和一些没有边框).
我还尝试了在stackoverflow上找到的建议:继承UILabel并执行此操作:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
BOOL isPDF = !CGRectIsEmpty(UIGraphicsGetPDFContextBounds());
if (!layer.shouldRasterize && isPDF)
[self drawRect:self.bounds]; // draw unrasterized
else
[super drawLayer:layer inContext:ctx];
}
Run Code Online (Sandbox Code Playgroud)
但这也没有改变任何事情.
无论我做什么,当在预览中打开PDF时,文本部分可以选择为块,但不是每个字符的字符,并且缩放pdf显示它实际上是位图图像.
有什么建议?
| 归档时间: |
|
| 查看次数: |
2803 次 |
| 最近记录: |