我试图从根本上了解如何在 iPhone 上创建简单的 PDF 并将其转换为 UIImage。我拼凑了这段代码:
NSMutableData *pdfData = [[NSMutableData alloc] init];
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)@"Text", NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, CGRectMake(0, 0, 612, 792));
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, [(NSString *)currentText length]),
framePath,
NULL);
CTFrameDraw(frame, UIGraphicsGetCurrentContext());
UIGraphicsEndPDFContext();
UIImage *pdfImage = [UIImage imageWithData:pdfData]
我相当确定这段代码是正确的,除了最后一行(这是不正确的,因为 UIImage 无法读取 PDF 数据),所以我试图找出我可以将我的 PDF 发送到哪个类。
'pdfData' 不是 nil,它的大小约为 5000 字节。
非常感谢你们能提供的任何帮助。干杯。
最佳答案
您可以使用 UIWebView 阅读 pdf 或 DocumentPreviewController。
关于iphone - 如何将 PDF NSData 绘制到 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6421092/