开发者

Problems saving PDF files in iOS4

开发者 https://www.devze.com 2023-01-12 12:53 出处:网络
I want to have my iPhone application create a PDF file and store it in an appropriate place - probably the iBooks folder so it can be easily retrieved using iTunes, but that\'s a change for later once

I want to have my iPhone application create a PDF file and store it in an appropriate place - probably the iBooks folder so it can be easily retrieved using iTunes, but that's a change for later once I have the saving part working. I've followed the PDF saving tutorial on the Apple site to the letter, including swapping out CGContextBeginPage and CGContextEndPage for their PDF specific equivalents, and I've checked that the save filename is set to the correct spot, but either no file is being created or I just can't find where it's being stored. Here's the code, any ideas what's wrong? Is it the code, or does this kind of operation just not run in the simulator?

    CGRect b = CGRectMake(0, 0, 612, 792);
NSMutableString *exportPath = [[NSMutableString alloc] 
                               initWithString:[[NSBundle mainBundle] bundlePath]];
[exportPath appendString:@"/myfile.pdf"];
CGContextRef pdfContext;
CFStringRef path = CFStringCreateWithCString开发者_Python百科 (NULL, [exportPath UTF8String], kCFStringEncodingUTF8);
CFURLRef url; CFMutableDictionaryRef myDictionary = NULL;

url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease(path);
myDictionary = CFDictionaryCreateMutable(NULL, 0,
                                         &kCFTypeDictionaryKeyCallBacks,
                                         &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));

pdfContext = CGPDFContextCreateWithURL (url, &b, myDictionary);
CFRelease(myDictionary);
CFRelease(url);

CGPDFContextBeginPage(pdfContext, NULL);
[self drawToContext: pdfContext Bounds:b];
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CGContextRelease (pdfContext);  


You are trying to save the PDF into your bundle. I'm pretty sure you can do that. Use the document path instead.

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *dbpath = [documentsDir stringByAppendingPathComponent:@"myfile.pdf"];
0

精彩评论

暂无评论...
验证码 换一张
取 消