Where to store a created PDF File?

大兔子大兔子 提交于 2020-01-03 03:39:09

问题


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!


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/8502181/where-to-store-a-created-pdf-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!