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

标签 ios objective-c pdf core-graphics pdfkit

尝试制作无法以任何方式修改的 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.

因此,根据我收集到的信息,我应该能够像这样将属性或属性组合在一起:(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)
};

关于ios - 带有选项字典崩溃应用程序的 UIGraphicsBeginPDFContextToFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58549717/

相关文章:

android - 谁 bundle 了 react-native 代码?

ios - 从 Firebase 获取 TableView 信息

ios - 如何判断字典键是 NSArray 还是 Bool 类型

iphone - "Data Formatters temporarily unavailable..."错误

java - 如何使用 PDFBox 的 PDFPagePanel 查看 PDF 文档

python - 从 PDF 文件中获取数据,其布局与复制+粘贴相同

RStudio/knitr 默认 PDF 查看器设置?

ios - self.navigationController.navigationBar.translucent = NO;在 navigationBar 之后创建一个额外的间隙

ios - 写类别时,我应该使用self还是类别名称

mysql - NSPredicate JOIN 两个表 OBJECTIVE C