我正在努力解决过去几个小时的问题。我以编程方式制作pdf并将其保存在〜/ library / Application Support / iPhone Simulator / ...并且程序在我的模拟器中正常工作。我可以在webView中保存和检索pdf。
但是当我在iPhone4设备上测试我的应用程序时,我在检索时看不到pdf文件。我是新手,所以我不知道手动检查在设备中创建的pdf与否。
请帮我。
感谢
我从这段代码中保存了pdf:
- (void)generatePdfButtonPressed{
pageSize = CGSizeMake(612, 792);
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[self generatePdfWithFilePath:pdfFileName];
}
- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
NSInteger currentPage = 0;
BOOL done = NO;
do
{
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
// Draw a page number at the bottom of each page.
currentPage++;
//[self drawPageNumber:currentPage];
//Draw a border for each page.
// [self drawBorder];
//Draw text fo our header.
[self drawHeader];
//Draw a line below the header.
[self drawLine];
//Draw personel details
[self drawPersonelDetail];
//Draw Education
[self drawEducation];
//Draw work Experiences
[self drawWorkExperiences];
//Draw work Activities
[self drawActivities];
//Draw Skills
[self drawSkils];
//Draw some text for the page.
// [self drawText];
yCord=550;
// [self drawText];
//Draw an image
// [self drawImage];
done = YES;
}
while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
答案 0 :(得分:2)
经过长时间的努力,我找到了自己的问题的解决方案:我在下面提供的解决方案可能对任何其他人有所帮助。
- (void)generatePdfButtonPressed{
pageSize = CGSizeMake(612, 792);
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[self generatePdfWithFilePath:pdfFileName];
}
- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
NSInteger currentPage = 0;
BOOL done = NO;
do
{
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
// Draw a page number at the bottom of each page.
currentPage++;
[self drawPageNumber:currentPage];
//Draw a border for each page.
[self drawBorder];
//Draw text our header.
[self drawHeader];
//Draw a line below the header.
[self drawLine];
//Draw personel details
[self drawPersonelDetail];
//Draw Education
[self drawEducation];
//Draw an image
[self drawImage];
done = YES;
}
while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
And for Display the pdf file the method is :
- (void)showPdfClicked
{
pdf1View.scalesPageToFit = YES;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"%@",documentsDirectory);
NSString *path1 =[documentsDirectory stringByAppendingPathComponent:@"Demo.pdf"];
NSLog(@"Retruve Save path is %@: ",path1);
// NSString *path1 = [[NSBundle mainBundle] pathForResource:@"pdf1" ofType:@"pdf"];
NSURL *targetURL1 = [NSURL fileURLWithPath:path1];
NSURLRequest *request1 = [NSURLRequest requestWithURL:targetURL1];
[pdf1View loadRequest:request1];
}
答案 1 :(得分:0)
我以前遇到过这个问题。我的猜测是内存问题 - 你要加载的PDF文件有多大? UIWebView
似乎有一些问题需要缓冲(如果这样做的话)大文件可以显示。
答案 2 :(得分:0)
我通过此代码通过电子邮件成功发送了我创建的pdf。可能这会帮助你(dottorfeelgood)。
-(IBAction)emailButtonClicked
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Subject"];
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
NSMutableData *pdfData = [NSMutableData data];
pdfData = [NSData dataWithContentsOfFile:pdfFileName];
CGRect bounds=CGRectMake(0, 0, 612, 792);
[picker addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"SomeFile.pdf"];
// Fill out the email body text
NSString *emailBody = @"Image is attached";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
//[picker release];
}
快乐编码!!!