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

UIGraphicsBeginPDFContextToFile with options dictionary crashing app

尝试制作无法以任何方式修改的 PDF 并测试不同的词典选项以了解它们的工作原理。问题是虽然文档说我可以使用一个东西,但它不允许我使用那个东西并且应用程序崩溃了。这是我的设置;

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

UIGraphicsBeginPDFContextToFile(self.savePath, CGRectZero, tempDict);

来自 UIGraphicsBeginPDFContextToFile 的文档:

A dictionary that specifies additional information to be associated with the PDF file. You can use these keys to specify additional metadata and security information for the PDF, such as the author of the PDF or the password for accessing it. The keys in this dictionary are the same keys you pass to the CGPDFContextCreate function and are described in the Auxiliary Dictionary Keys section of CGPDFContext. The dictionary is retained by the new context, so on return you may safely release it.

来自 kCGPDFContextAccessPermissions 的文档

/* The document's access permissions, expressed as a CFNumber. The number is defined by ORing together the desired CGPDFAccessPermissions values. */

CG_EXTERN const CFStringRef kCGPDFContextAccessPermissions
CG_AVAILABLE_STARTING(10.13, 11.0);

来自 CGPDFDocument 的文档

// To get access permissions from a CGPDFDocument, call CGPDFDocumentGetAccessPermissions. Setting permissions // can only be done using the kCGPDFContextAccessPermissions property in the auxiliary info dictionary passed // in to 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 。

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

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

NSDictionary *tempDict = @{
    (NSString *)kCGPDFContextOwnerPassword : @"user",
    (NSString *)kCGPDFContextAccessPermissions : @(kCGPDFAllowsCommenting | kCGPDFAllowsLowQualityPrinting)
};