Rotate PDF to UIWebView with Swift

I have a view that is used to display different PDF files in a UIWebView , this UIWebView loads the PDF file as NSData , for example:

 webView.loadData(pdfData, MIMEType: "application/pdf", textEncodingName: "utf-8", baseURL: NSURL()) 

Now there are several PDF files that need to be rotated using the UIButton , which is located above the UIWebView , is there a way to rotate the PDF inside the UIWebView ?

What I tried so far was

webView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2) )

but this did not lead to the fact that I was looking for a result.

+5
source share
3 answers
  • To rotate a single view controller in an application, override the shouldAutorotate method.
  • Check the current statusBarOrientation , then update the UIInterfaceOrientation as follows: -

     let interfaceOrienation : UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation if(interfaceOrienation == UIInterfaceOrientation.Portrait){ let value = UIInterfaceOrientation.LandscapeLeft.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation") }else{ let value = UIInterfaceOrientation.Portrait.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation") } 

I have developed a sample project for you. Please follow the link below to download full code . https://www.dropbox.com/s/3hk5y9g7v74cbkd/Stackoverflow_webview.zip?dl=0

enter image description here

+6
source

If I understand your problem, to center your PDF after rotating it to display it correctly on top, unfortunately CGAffineTransformMakeRotation not enough.

You can do:

 webView.scalesPageToFit = true let scale = CGAffineTransformMakeScale(0.6, 0.6) let translate = CGAffineTransformMakeTranslation(webView.frame.origin.x, webView.frame.origin.y - webView.frame.size.height * 0.25) let transform = CGAffineTransformConcat(translate, scale); webView.transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2)) 

And return from conversion:

 webView.transform = CGAffineTransformIdentity 

There is also another working method, but I do not recommend doing :

(Too long and too difficult to process the html page to make a rotation.)

install webView -> download responsive HTML page -> this flexible download HTML PDF page -> insert the following javascript into webView

 func webViewDidFinishLoad(webView: UIWebView) { let js = "document.body.style.setProperty('-webkit-transform', 'rotate(-90deg)', null);" webView.stringByEvaluatingJavaScriptFromString(js) } 

You can also check your JS and JSFiddle if you want.

+3
source

I am currently using this code for rotation, but "the view does not remain centered (I think based on my layout)

 print(webView.transform) if (webView.transform.a == 1.0 && webView.transform.d == 1.0) {webView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI/2));print("done1")} else if(webView.transform.b == 1.0 && webView.transform.c == -1.0) {webView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI));print("done2")} else if(webView.transform.a == -1.0 && webView.transform.d == -1.0) {webView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI+(M_PI/2)));print("done3")} else if(webView.transform.b == -1.0 && webView.transform.c == 1.0) {webView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI + M_PI));print("done4")} else{print("nothing happens")} 

Vid example http://imgur.com/O2Qjuu2

+1
source

Source: https://habr.com/ru/post/1246093/


All Articles