简体   繁体   中英

Convert WKWebView to CIImage

I want to convert WKWebView or UIWebView to CIImage . How can I do it?

UIGraphicsBeginImageContext(image.extent.size)
self.webview.layer.render(in:UIGraphicsGetCurrentContext()!)
let result: CIImage = CIImage(image: 
UIGraphicsGetImageFromCurrentImageContext()!, options: nil)!
    UIGraphicsEndImageContext()

I did it like this, but it does not work.I expect that the contents of UIWebView will be displayed in CIImage . Background color has been displayed.

This can be achieved in 2 steps :

  1. Convert WKWebView (having base class as UIView ) to UIImageView

     extension UIImage { convenience init(view: UIView) { UIGraphicsBeginImageContext(view.frame.size) view.layer.render(in:UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() self.init(cgImage: image!.cgImage!) } } 
  2. Convert the UIImage received from step 1 to CIImage

     let ciimage = CIImage(image: image) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM