我正在尝试在iPhone上创建PDF以供练习。我不确定为什么它不起作用。我收到错误:
2011-09-21 21:35:00.412 CreatePDF[869:b303] -[CreatePDFViewController createPDFDocumentAtURL:]
CreatePDF[869] <Error>: CGDataConsumerCreateWithFilename: failed to open `/Users/jon/Library/Application Support/iPhone Simulator/4.3.2/Applications/4486CF27-B309-4C7C-82A5-B425B9E2C04D/Documents' for writing: Is a directory.
deflateEnd: error -3: (null).
CreatePDF[869] <Error>: CGPDFContextCreate: failed to create PDF context delegate.
2011-09-21 21:35:00.419 CreatePDF[869:b303] Couldn't create PDF context
<Error>: CGContextBeginPage: invalid context 0x0
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *documentsURL = [NSURL fileURLWithPath:documentsDirectory];
[self createPDFDocumentAtURL:(CFURLRef)documentsURL];
NSLog(@"%s", __FUNCTION__);
}
- (void)createPDFDocumentAtURL:(CFURLRef)url {
NSLog(@"%s", __FUNCTION__);
float red[] = {1., 0., 0., 1. };
CGRect mediaBox = CGRectMake(0, 0, 850, 1100);
CGContextRef pdfContext = CGPDFContextCreateWithURL(url, &mediaBox, NULL);
if (!pdfContext) {
NSLog(@"Couldn't create PDF context");
}
CGContextBeginPage(pdfContext, &mediaBox);
CGContextSaveGState(pdfContext);
CGContextClipToRect(pdfContext, mediaBox);
CGContextSetFillColorSpace(pdfContext, CGColorSpaceCreateDeviceRGB());
CGContextSetFillColor(pdfContext, red);
CGContextFillRect(pdfContext, mediaBox);
CGContextRestoreGState(pdfContext);
CGContextEndPage(pdfContext);
CGContextRelease(pdfContext);
}
答案 0 :(得分:2)
您必须将新文件的名称添加到您的网址中。您的 documentsURL 现在包含此内容:
/ Users / jon / Library / Application Support / iPhone Simulator / 4.3.2 / Applications / 4486CF27-B309-4C7C-82A5-B425B9E2C04D / Documents /