UIGraphicsBeginPDFContextToFile,带有选项字典崩溃的应用程序

问题描述 投票:0回答:1

[尝试制作无法以任何方式修改的PDF,并测试不同的词典选项以查看其工作方式。问题是,尽管文档说我可以使用一件事,但不允许我使用那件事,并且应用程序崩溃。这是我的设置;

NSDictionary *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:@"user", kCGPDFContextOwnerPassword, (kCGPDFAllowsCommenting | kCGPDFAllowsLowQualityPrinting), kCGPDFContextAccessPermissions, nil];

UIGraphicsBeginPDFContextToFile(self.savePath, CGRectZero, tempDict);

来自UIGraphicsBeginPDFContextToFile的文档:

指定要关联的其他信息的字典PDF文件。您可以使用这些键来指定其他PDF的元数据和安全信息,例如PDF的作者PDF或用于访问它的密码。这本字典中的键与您传递给CGPDFContextCreate函数的键相同,并且在CGPDFContext的“辅助字典关键字”部分中进行了介绍。该词典被新上下文保留,因此返回时,您可以安全地释放它。

来自kCGPDFContextAccessPermissions的文档

/ *文档的访问权限,表示为CFNumber。的通过将所需的或运算在一起定义数字CGPDFAccessPermissions值。 * /

CG_EXTERN const CFStringRef kCGPDFContextAccessPermissionsCG_AVAILABLE_STARTING(10.13,11.0);

摘自CGPDFDocument的文档

//要从CGPDFDocument获得访问权限,请致电CGPDFDocumentGetAccessPermissions。设置权限//使用kCGPDFContextAccessPermissions属性完成辅助信息字典// //传入CGPDFContextCreate。

因此,从我收集的信息中,我应该能够像这样对属性进行OR运算:(kCGPDFAllowsCommenting | kCGPDFAllowsLowQualityPrinting),以便将这些属性推入PDF创建辅助词典中,然后能够使用或不使用那些权限来编写PDF。另一个问题是,如果我不想打开或关闭某些权限怎么办?权限如下:

typedef CF_OPTIONS(uint32_t, CGPDFAccessPermissions) {
    kCGPDFAllowsLowQualityPrinting    = (1 << 0),   // Print at up to 150 DPI
    kCGPDFAllowsHighQualityPrinting   = (1 << 1),   // Print at any DPI
    kCGPDFAllowsDocumentChanges       = (1 << 2),   // Modify the document contents except for page management
    kCGPDFAllowsDocumentAssembly      = (1 << 3),   // Page management: insert, delete, and rotate pages
    kCGPDFAllowsContentCopying        = (1 << 4),   // Extract content (text, images, etc.)
    kCGPDFAllowsContentAccessibility  = (1 << 5),   // Extract content, but only for the purpose of accessibility
    kCGPDFAllowsCommenting            = (1 << 6),   // Create or modify annotations, including form field entries
    kCGPDFAllowsFormFieldEntry        = (1 << 7)    // Modify form field entries
};

我希望将kCGPDFAllowsDocumentChanges字段关闭,因此无法对PDF进行任何更改。我试图通过PDFKit编写带有选项的文件来做到这一点,我也尝试了上述方法,并且发生了同样的事情。我可以编写CGPDFContext.h中包含的所有其他选项,但是如果不崩溃,就无法编写kCGPDFContextAccessPermissions的权限。

回答任何大的帮助将不胜感激,因为零文档或使kCGPDFAllowsDocumentChanges失效的代码示例完全存在。

ios objective-c pdf core-graphics pdfkit
1个回答
0
投票

您正在尝试在int中存储普通的uinit32_t(实际上是NSDictionary)。你不能那样做。您只能将对象存储在NSDictionary中。因此,您需要将值包装在NSNumber中。

使用现代的Objective-C,您的代码将是:

NSDictionary *tempDict = @{
    kCGPDFContextOwnerPassword : @"user",
    kCGPDFContextAccessPermissions : @(kCGPDFAllowsCommenting | kCGPDFAllowsLowQualityPrinting)
};
© www.soinside.com 2019 - 2024. All rights reserved.