UITextView with HTML: How to display image in local

1.7k Views Asked by At

To display html in uitextview, I used NSAttributedString. It seems OK, except loading an image from local by <img src=/var/mobile/Containers/... is not working.

What am I doing wrong?

// Save image in document
// Save path to imgPath
self.textContent = "<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src='\(imgPath)' width=200 height=200 >"
if let htmlData = self.textContent.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: false) {
    var attributedString: NSAttributedString = NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil, error: nil)!
    self.txtView.attributedText = attributedString;
}
1

There are 1 best solutions below

0
On
self.textContent = "<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src="file:///var/mobile/....." width=200 height=200 >"  

set image src start with "file://" may solve this problem.