2016-08-11 2 views
1

내 프로젝트에서는 PDFJS 라이브러리를 사용하고 있습니다. 나는 UIWebView에 현지 pdf를 적재하고있다. 그러나 이것은 많은 RAM 메모리를 차지하며 한 시점에서 충돌을 일으 킵니다. 이를 피하려면 WKWebView을 사용하고 싶습니다.iOS에서 pdf로 html 파일을로드하는 방법 WKWebView

UIWebview에서

, I는 다음과 같이 사용하고, I는 상기 단편에 finalPath를 인쇄 할 때 콘솔 출력 WKWebView, loadFileURL에서 /var/containers/Bundle/Application/DF419672-CF14-4B60-BE4F-EC0AC07C23AE/WebviewPOC.app/PDFJS/web/viewer.html?file=/var/containers/Bundle/Application/DF419672-CF14-4B60-BE4F-EC0AC07C23AE/WebviewPOC.app/swift_tutorial.pdf#page=1

이다

UIWebView *uiWebview = [[UIWebView alloc] initWithFrame:self.frame]; 
[self addSubview:uiWebview]; 
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"swift_tutorial" ofType:@"pdf"]; 
NSString *sPath = [[NSBundle mainBundle] pathForResource:@"viewer" ofType:@"html" inDirectory:@"PDFJS/web"]; 
NSString *finalPath = [NSString stringWithFormat:@"%@?file=%@#page=1",sPath,filePath]; 
self.urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:finalPath]]; 
[uiWebview loadRequest:self.urlRequest]; 

(selfUIView의 서브 클래스를 의미) loadHTMLString 메서드는 로컬 html 파일이나 로컬 pdf 파일을로드하는 데 사용할 수 있습니다.이 파일은 정상적으로 작동합니다. 그러나 둘 다. 이 html 파일의 경우 로컬 PDF 경로와로드를 WKWebView에 추가하는 방법은 무엇입니까?

도움을 주시면 감사하겠습니다.

답변

0

나 자신의 질문에 대답하게 해주세요.

로컬 호스트 서버를 만들기 위해 WKWebViewLocal 라이브러리를 사용했습니다. 이제 호스트 이름을 통해 로컬 파일에 액세스 할 수 있습니다. 이 접근 방식을 사용하면 앱의 메모리 사용률이 많이 최적화되었습니다 (WKWebView 덕분에).

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"swift_tutorial" ofType:@"pdf"];  
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"viewer" ofType:@"html" inDirectory:@"PDFJS/web"]; 
NSString *finalPath = [NSString stringWithFormat:@"http://localhost:8080%@?file=%@#page=1",htmlPath, filePath]; 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:finalPath]]; 
[self.webView loadRequest:request]; 

이제 finalpath이 될 것입니다

http://localhost:8080/~~~~~~~/PDFJS/web/viewer.html?file=/Users/~~~~~~/swift_tutorial.pdf#page=1 
관련 문제