Я пытаюсь создать PDF на iPhone для практики. Я не уверен, почему это не работает. Я получаю ошибку:
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);
}