gpt4 book ai didi

ios - 在 webview 中创建和加载 pdf

转载 作者:行者123 更新时间:2023-11-29 00:29:10 27 4
gpt4 key购买 nike

我正在尝试创建 PDF,然后在 webview 中加载 pdf 以供预览。现在我可以成功创建pdf了。接下来我想通过按下按钮创建 pdf,然后加载到 webview 中。我引用了 this linkthis link .虽然pdf创建成功,但是在webview中无法加载pdf。有人可以提供建议吗?非常感谢。

代码:

-(IBAction)preview:(id)sender
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
NSDate*todayDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-yyyy "];
NSString *mytext = [NSString stringWithFormat:@"form%@",[dateFormatter stringFromDate:todayDate]];
[self createPDFfromUIView:_myView saveToDocumentsWithFileName:mytext];
NSLog(@"is saved in %@", mytext);
NSString *path = [self createPDFfromUIView:_myView saveToDocumentsWithFileName:mytext];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
}
-(NSMutableData *)createPDFDatafromUIView:(UIView*)aView
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[aView.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
return pdfData;
}
-(NSString*)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
NSMutableData *pdfData = [self createPDFDatafromUIView:aView];
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[aView.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
NSLog(@"saved in %@", documentDirectoryFilename);
return documentDirectoryFilename;
}

最佳答案

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];

我没有看到您实际将 UIWebView 添加为 subView 的任何地方。

[self.view addSubview:webView];

还有。您应该将 UIWebView 添加到 property 而不是每次有人按下按钮时都分配/创建它。

@property (strong, nonatomic) UIWebView *webView;

最后:

您应该使用 WKWebView而不是 Apple 文档中提到的 UIWebView:

Starting in iOS 8.0 and OS X 10.10, use WKWebView to add web content to your app. Do not use UIWebView or WebView.

编辑:

对于真正遇到问题的阅读本文的人,因为这个问题实际上意味着检查关于同一问题的其他线程:

Load text+pdf file in uiwebview

iPhone : can we open pdf file using UIWebView?

how to load pdf file in UIWebview center

Display local pdf file in UIWebView in iphone

Loading Local PDF File Into WebView

关于ios - 在 webview 中创建和加载 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42317855/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com