Copy img from a WKWebView

Hello,


I have a WKWebView which contains an HTML from the net that has an <img> tag (with its source also from the net). Right, what I would like to do is to get that image in an UIImage object. Is there any way to do this? Or I have to pass the image URL through userContentController and manually dowload that again? (Wanted to avoid double downloads...)


Thanks,

Tamas

WKWebView provides no way to get an image object (UIImage, CGImage, whatever) from an HTML image. You can, as you’ve already noted, pass the URL to native code and re-download it. Alternatively:

  • If you control the web site you could change the HTML to not use an

    IMG
    element but instead reference the image dynamically. At that point you can have your code (either native code or JavaScript code) download the image data and share that data with both sides.
  • You could render the image on the web side and then pass that rendered version to your native side. I don’t know enough modern web APIs to explain the details here, but the HTML5 Canvas element seems like a good place to start.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Copy img from a WKWebView
 
 
Q