0

I have a toolbar with a screenshot button. This works great, but I want to take the screenshot without the toolbar in view. I've tried code that I've found on SO, but the image is huge and I can't tell what the screenshot is of since it appears to only screenshot the top left corner. I also have another function in a different part of the app that allows you to email the screencapture. This is huge when it's read in, so I resize it before attaching to the email. This works well, but I'm not sure how to resize and then crop the screenshot without the toolbar (highlighted in Red):

screenshotImage1

I'd like to take the screenshot without the toolbar. I'm still learning, so I've played around with UIImage and CGRect, but unsuccessful.

 let imageScreenshot = view.snapshot() let screenWidth = self.view.frame.size.width let screenHeight = self.view.frame.size.height let cropSize = CGRect(x: 0.0, y: 0.0, width: screenWidth, height: screenHeight * 0.75) let imageHolder = imageScreenshot.cgImage?.cropping(to: cropSize) let screenshotOfWindow = UIImage(cgImage: imageHolder!) extension UIView { func snapshot() -> UIImage { UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, UIScreen.main.scale) self.layer.render(in: UIGraphicsGetCurrentContext()!) let img = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return img } } 

After the suggestion to take a screenshot with webkit:

 webView!.takeSnapshot(with: nil, completionHandler: { (image, error) in self.screenshotOfWindow = image self.showScreenshotEffect() }) 

Actually, it doesn't appear to save to Core Data after this change, but the snapshot is taken.

This code hits on the first line and bypasses all others:

 webView.takeSnapshot(with: nil, completionHandler: { (image,error) in if let image = image { DispatchQueue.main.async { self.webView.isHidden = true self.screenshotOfWindow = image } } else { print (error?.localizedDescription as Any) } }) 
3
  • Show the code you've tried Commented May 16, 2019 at 1:22
  • Are you using webview? Commented May 16, 2019 at 1:37
  • I'm using webview. Commented May 16, 2019 at 2:37

1 Answer 1

1

Use takeSnapshot(with:completionHandler:) method to take snapshot of wkwebview. And use the completion block runs in background thread. So change the image in main thread.

class ViewController: UIViewController { @IBOutlet weak var webView: WKWebView! @IBOutlet weak var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white webView.load(URLRequest(url: URL(string: "https://www.facebook.com")!)) } @IBAction func buttonAction(_ sender: UIButton) { webView.takeSnapshot(with: nil) { (image, error) in if let image = image { DispatchQueue.main.async { self.webView.isHidden = true self.imageView.image = image } } else { print(error?.localizedDescription) } } } } 
Sign up to request clarification or add additional context in comments.

8 Comments

Okay. This isn't working. I'm getting blank images now, but the function appears to be working:
@Nutrion call this method after the webview is loaded
Correction: the snapshot isn't working. I'll keep playing with it and report back.
This works if I save to the camera roll, but it is not working trying to save the screenshot to a UIImage
@Nutrion You should use main thread to change the image. Check updated answer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.