【问题标题】:UIGraphicsBeginPDFContextToFile with options dictionary crashing app带有选项字典崩溃应用程序的 UIGraphicsBeginPDFContextToFile
【发布时间】:2020-02-21 06:52:45
【问题描述】:

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

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

UIGraphicsBeginPDFContextToFile(self.savePath, CGRectZero, tempDict);

来自 UIGraphicsBeginPDFContextToFile 的文档:

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

来自 kCGPDFContextAccessPermissions 的文档

/* 文档的访问权限,以 CFNumber 表示。这 数字由 ORing 一起定义所需的 CGPDFAccessPermissions 值。 */

CG_EXTERN const CFStringRef kCGPDFContextAccessPermissions
CG_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】:

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

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

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

    【讨论】:

    • 然而,唯一的另一个问题是我将如何关闭这些选项,也许我将如何传递一个空字典?这是真正的问题,我需要关闭 kCGPDFAllowsDocumentChanges 的条目。我知道这是可能的,只是不知道语法,这让我大吃一惊。
    • 其实我已经解决了,改正错误,你有正确的答案,谢谢你的帮助,shodl 一直是一个nobrainer
    猜你喜欢
    • 1970-01-01
    • 2014-01-14
    • 2017-09-27
    • 2014-10-28
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    相关资源
    最近更新 更多