【问题标题】:Where to store a created PDF File?在哪里存储创建的 PDF 文件?
【发布时间】:2012-01-20 02:12:11
【问题描述】:

得到以下错误:

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 的东西,它也不起作用。

谢谢!

【问题讨论】:

    标签: ios xcode pdf store


    【解决方案1】:

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

    - (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"];
    

    【讨论】:

    • 感谢您的回答。它不适用于我的类方法?做- (NSString *) ... 就像一个魅力。
    • 哎呀,不应该是类方法对不起。 (我从我的一个库中复制了代码,它一个类方法)
    • 我需要这方面的帮助..stackoverflow.com/questions/16646039/…。你能建议吗
    【解决方案2】:

    扩展亚历克斯的答案

    文档目录是您应该考虑存储 PDF 的唯一位置。它是 Apple 保证您的应用程序创建的东西的持久性以及在升级等之间持久存在的唯一文档存储。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-24
      • 2020-02-19
      • 1970-01-01
      • 2017-01-06
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多