在哪里存储创建的 PDF 文件?

发布于 2024-12-21 03:54:35 字数 709 浏览 1 评论 0原文

出现以下错误:

CGDataConsumerCreateWithFilename: failed to open `/name' for writing: Permission denied.

使用此代码(不是我的):

- (void)createPDFWithName
{
//NSURL * newFilePath = [[NSURL alloc] initFileURLWithPath:name];
NSString * newFilePath = @"name";

CGRect page = CGRectMake(0, 0, 300, 300);

NSDictionary * metaData = nil;

if (!UIGraphicsBeginPDFContextToFile(newFilePath, page, metaData )) {
    NSLog(@"error creating PDF context");
    return;
}

UIGraphicsBeginPDFPage();
[self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndPDFContext();
}

那么: 我应该在哪里存储此 PDF 文件?我尝试了 NSBundle MainBundle ,但它也不起作用。

谢谢你!

Got the following error:

CGDataConsumerCreateWithFilename: failed to open `/name' for writing: Permission denied.

With this code (not mine):

- (void)createPDFWithName
{
//NSURL * newFilePath = [[NSURL alloc] initFileURLWithPath:name];
NSString * newFilePath = @"name";

CGRect page = CGRectMake(0, 0, 300, 300);

NSDictionary * metaData = nil;

if (!UIGraphicsBeginPDFContextToFile(newFilePath, page, metaData )) {
    NSLog(@"error creating PDF context");
    return;
}

UIGraphicsBeginPDFPage();
[self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndPDFContext();
}

So: Where should I store this PDF-File? I tried the NSBundle MainBundle thing and it did not work either.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下壹個目標 2024-12-28 03:54:35

您应该了解文件系统上可以创建文件的各个位置。其中之一是文档路径,示例如下。

- (NSString *)documentPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return documentPath;
}

NSString * newFilePath = [[self documentPath] stringByAppendingPathComponent:@"name.pdf"];

You should read about the various places on the file system that you can create files. One of these is the documents path, an example is below.

- (NSString *)documentPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return documentPath;
}

NSString * newFilePath = [[self documentPath] stringByAppendingPathComponent:@"name.pdf"];
梦里兽 2024-12-28 03:54:35

扩展 Alex 的答案

文档目录是您应该考虑存储 PDF 的唯一位置。它是 Apple 保证您的应用程序创建的内容持久存在并将在升级等之间保留的唯一文档存储。

您无法将内容推送到 MainBundle 中,因为该捆绑包在应用程序的整个生命周期中保持不变

Expanding on Alex's answer

The documents directory is the ONLY place you should be thinking of storing your PDF. It is the only document store that Apple guarentees persistence of things your app creates and will persist between upgrades etc.

You can't push stuff into your MainBundle as the bundle stays the same throughout the life of your app

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文