2013-03-08 171 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];