有很多关于如何在App‘sUIView中显示PDF的资源.我现在的工作是从UIViews创建一个PDF.

例如,我有一个UIView,带有文本视图、UILabelsUIImages等子视图,那么如何将big UIView作为一个整体,包括其所有子视图和子视图转换为PDF?

我已经查了Apple's iOS reference张了.然而,它只谈到将文本/图像片段写入PDF文件.

我面临的问题是,我想要以PDF格式写入文件的内容很多.如果我把它们一块一块地写到PDF中,那将是一项巨大的工作. 这就是为什么我正在寻找一种方法来写入UIViews到PDF,甚至位图.

我已经try 了从Stack Overflow中的其他Q/A复制的源代码.但它只给我一个UIView边界大小的空白PDF.

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // 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();

    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
    [aView drawRect:aView.bounds];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

推荐答案

请注意,以下方法创建了just a bitmap个视图;它不会创建实际的排版.

(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // 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();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

另外,请确保导入: QuartzCore/QuartzCore.h

Ios相关问答推荐

连接一些字符串,每个字符串都应该可以在swiftUI中单独点击

UIBezierPath包含:方法不检测到厚度大于1像素的线上的touch

AlGolia InstantSearch Swift Package Manager引发编译错误

Swift导航远离视频导致崩溃

视图未更新以在按下按钮时显示另一个视图

mapView swift 导致警告有网格错误

如何在集合视图中创建装饰视图?

在Xcode 15中为具有@Binding的视图创建SwiftUI #预览的方法

NumberFormatter 无法使用 Xcode 15.0 beta 正确识别 iOS 17 beta 中的 Locale 货币设置

核心媒体基础知识,了解 CMSampleBuffer、CMBlockBuffer 和 CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer

如何在 SwiftUI 中同时使用 LongPressGesture 和 ScrollView 中的滚动?

一对多关系 Firebase 实时数据库(嵌入数百万条 comments )

叠加视图不在堆栈中维护自身 - SwiftUI

Xcode 不支持 iOS 15.6

Text() 正在添加额外的前导尾随填充 SwiftUI

iOS SwiftUI - 从以前的 struct 调用函数

在 iPad 上删除列表的顶部和底部行

从 NSUserDefaults 字典 iOS 中删除所有键

使用 Xcode 4 的 iPhone 临时构建

如何进行键值观察并在 UIView 的框架上获取 KVO 回调?