2013-03-08 131 views
0

PDF文件我在做一个application.In,我感到显示在网页视图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]; 

但是当我点击第一次打开它会崩溃的应用程序,并从下一次起它在第一时间为O打开correctly.And得到了错误,如

dyld: fast lazy binding from unknown image 

所以,请告诉我如何不崩溃,显示在UIWebView的PDF文件。

回答

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]; 
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]; 
} 
+0

文件不可用在我app.File在应用程序内存(目录)可用。 – 2013-03-08 07:23:14

+0

好的,那么我们必须从NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)获取路径; – Vedchi 2013-03-08 07:29:01

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]; 
+0

dataWithContentOfFile需要nsstring值而不是url。 – 2013-03-08 07:22:23

+0

对,...在回答的比赛中忘记了... – BhushanVU 2013-03-08 07:24:43

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];