Nutrion
Nutrion

Reputation: 63

Screenshot without toolbar in view

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)
                }
        })

Upvotes: 0

Views: 226

Answers (1)

RajeshKumar R
RajeshKumar R

Reputation: 15758

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)
            }
        }
    }
}

Upvotes: 1

Related Questions