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 만 포함됩니다 (일부는 테두리가 있고 일부는 테두리가 없음). 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];
}
을하지만 그 중 하나가 아무것도 변경하지 않았다.
미리보기에서 PDF를 열 때 텍스트 부분은 블록으로 선택할 수 있지만 문자 당 문자는 선택할 수없고 PDF 확대/축소는 실제로 비트 맵 이미지로 표시됩니다.
제안 사항?
예제 코드 조각과 출력 PDF 파일을 게시하여 볼 수 있습니까? – iPDFdev
혹시 이것을 알아 냈습니까? "블록 선택"문제가 발생했습니다. – JeremyDay
이제는 발생하지 않습니다. 'UIGraphicsBeginPDFContextToFile (outputPath, view.bounds, nil); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [view.layer renderInContext : pdfContext]; UIGraphicsEndPDFContext(); ' – JeremyDay