1

I have a UIImageView in my app. I want to display it's image in some html content which I am rendering in app. This is a small html code which displays image in html :

<td class="title"> <img src="#LOGO_IMAGE#" style="width:100%; max-width:300px; background-color: #cdcdcd">
</td>  

This is what I am doing in Swift to get url (I am aware this is incorrect) :

let logoString = String(data: imageData, encoding: .utf8)
HTMLContent = HTMLContent.replacingOccurrences(of: "#LOGO_IMAGE#", with: logoString!)  

Line number 1 gives me nil.
How may I fix this ?

Nitish
  • 13,845
  • 28
  • 135
  • 263

2 Answers2

0
let base64String= imageData.base64EncodedStringWithOptions(.allZeros)
let htmlString = "data:image/png;base64, \(base64String)"

You might then load the htmlString.

Morty Choi
  • 2,466
  • 1
  • 18
  • 26
0

Assuming you have a UIImageView named "theImageView" and a UIWebView named "the WebView", this should do it...

    var myHTMLString = "<html><head></head><body bgcolor=#00ffff><p>This is a test</p><p><img src=\"#LOGO_IMAGE#\" style=\"width:100%; max-width:300px; background-color: #cdcdcd\"></p></body></html>"

    if let img = theImageView.image {

        if let pngRepA = UIImagePNGRepresentation(img) {

            let strBase64 = pngRepA.base64EncodedString(options: [])

            let logoString = "data:image/png;base64, \(strBase64)"

            myHTMLString = myHTMLString.replacingOccurrences(of: "#LOGO_IMAGE#", with: logoString)

        }

    }

    theWebView.loadHTMLString(myHTMLString, baseURL: nil)
DonMag
  • 69,424
  • 5
  • 50
  • 86