问题
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