0

我正在做一个应用程序。我在 webview 中显示 pdf 文件,如下所示

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[default1 objectForKey:@"KeyToSelectedFile"]];
NSString *filename1=[fileName stringByAppendingPathComponent:s1];

NSLog(@"%@",filename1);
NSURL *url = [NSURL fileURLWithPath:filename1];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
web=[[UIWebView alloc]initWithFrame:CGRectMake(0,0, 1020, 748)];
web.delegate=self;
[web loadRequest:request];
 web.backgroundColor=[UIColor whiteColor];
[self.view addSubview:web];

但是,当我第一次单击打开时,它会使应用程序崩溃,并且从下一次开始它会正确打开。并且第一次出现类似的错误

dyld: fast lazy binding from unknown image

所以请告诉我如何在 uiwebview 中显示 pdf 而不会崩溃。

4

4 回答 4

1

我检查了这个,它工作正常,试试

UIWebView * pdfWebView = [[UIWebView alloc] initWithFrame:'your Frame'];
NSArray * Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * bundlePath = [[Paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",yourFileName]];

if([[NSFileManager defaultManager] fileExistsAtPath:bundlePath])
{
    NSLog(@"Path : %@",bundlePath);
    [pdfWebView loadData:[NSData dataWithContentsOfFile:bundlePath] MIMEType:@"application/pdf" textEncodingName:nil baseURL:nil];
    [self.view addSubview:pdfWebView];
}
于 2013-03-08T07:15:51.920 回答
0

这将完美无缺。

UIWebView * webview = [[UIWebView alloc] initWithFrame:Frmaesize
NSString * path = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"pdf"];
NSURL * URL = [NSURL path];
NSURLRequest * request = [NSURLRequest requestWithURL:URL];
[webview loadRequest:request];
[self.view addsubView:webview];
于 2013-03-08T07:19:32.867 回答
0
NSData *data = [NSData dataWithContentsOfFile: filename1];
[web loadData:data MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
web.backgroundColor=[UIColor whiteColor];
[self.view addSubview:web];
于 2013-03-08T07:16:38.330 回答
0

为您的代码尝试这种方式,

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[default1 objectForKey:@"KeyToSelectedFile"]];
        NSString *filename1=[fileName stringByAppendingPathComponent:s1];

        web=[[UIWebView alloc]initWithFrame:CGRectMake(0,0, 1020, 748)];
        web.delegate=self;
        web.backgroundColor=[UIColor whiteColor];
        NSData *data = [NSData dataWithContentsOfFile:filename1];
        [web loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:url];
        [self.view addSubview:web];
于 2013-03-08T07:20:59.607 回答