iOS 通过创建 pdf 获取错误 [英] iOS get error by creating a pdf

查看:39
本文介绍了iOS 通过创建 pdf 获取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 pdf 文件,但出现 2 个错误:

i want to create a pdf file, but i get 2 errors:

<Error>: CGDataConsumerCreateWithFilename: failed to open `/test.pdf' for writing:   Operation not permitted. deflateEnd: error -3: (null).

还有这个

<Error>: CGPDFContextCreate: failed to create PDF context delegate.

我计算控制器类中的路径

I compute the path in the controller class

- (void)viewDidLoad
{
    documentName = @"test.pdf";
    NSArray *arrayPaths =
    NSSearchPathForDirectoriesInDomains(
                                    NSDocumentDirectory,
                                    NSUserDomainMask,
                                    YES);
    NSString *path = [arrayPaths objectAtIndex:0];
    NSString* pdfFileName = [path stringByAppendingPathComponent:documentName];
    url = [NSURL fileURLWithPath:pdfFileName];

    PDFRenderer *pdf = [[PDFRenderer alloc]initWithFileName:documentName];
    [pdf renderPDF];


    [super viewDidLoad];
}

在渲染类中我调用

UIGraphicsBeginPDFContextToFile(documentName, CGRectZero, nil);

函数,我得到错误.

我如何修复该错误?

推荐答案

你写的是相对路径,尝试改成

You are writing to a relative path, try to change it into

PDFRenderer *pdf = [[PDFRenderer alloc]initWithFileName:pdfFileName];

即文档目录中的绝对路径.

i.e. the absolute path in the documents directory.

如果您查看错误消息,您将看到正在尝试将文件写入根目录.

If you look at the error message, you will see that the file is being attempted to be written in the root directory.

这篇关于iOS 通过创建 pdf 获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆