Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView with HTML: How to display image in local

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;
}
like image 963
HoanNguyen Avatar asked Oct 22 '25 03:10

HoanNguyen


1 Answers

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.

like image 104
wtracyliu Avatar answered Oct 23 '25 19:10

wtracyliu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!