私はiOS 6アプリケーションで見栄えのよいPDFを生成したいと考えています。私が試したiOS上に「実際の」テキストコンテンツを含むPDFを生成するにはどうすればよいですか?
:
- のUIViewをここでUILabelのdrawRect に
を使用してNSStringのdrawInRect
-(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);
}
レンダリングされるUIViewには、UIImageView +一束のUILabels(一部には境界線があり、境界線がないもの)のみが含まれます。
また、私はstackoverflowの上で見つかった提案しようとした:
- (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];
}
しかし、それはどちらか何も変更しませんでした:UILabelをサブクラス化し、これを行うことを。
プレビューでPDFを開くと、テキストパーツはブロック単位で選択できますが、文字単位では選択できず、pdfをズームすると実際にはビットマップイメージになります。
提案がありますか?
サンプルコードの断片と出力PDFファイルを投稿できるので、それらを見ることができますか? – iPDFdev
あなたはこれを理解したことがありますか? 「ブロック選択」問題を経験する。 – JeremyDay
と表示されません。 'UIGraphicsBeginPDFContextToFile(outputPath、view.bounds、nil); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [view.layer renderInContext:pdfContext]; UIGraphicsEndPDFContext(); ' – JeremyDay