Convert HTML Content to image

点点圈 提交于 2019-12-04 10:49:59

Try this solution using label/UITextView

set html text to your label.

do {
let attrStr = try NSAttributedString(
    data: "<b><i>text</i></b>".data(using: String.Encoding.unicode, allowLossyConversion: true)!,
    options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
    documentAttributes: nil)
yourLabel.attributedText = attrStr
} catch let error {
  print(error)
}

to take snapshot try below code

UIGraphicsBeginImageContextWithOptions(yourLabel.bounds.size, 
false, 0);
yourLabel.drawViewHierarchyInRect(yourLabel.bounds, afterScreenUpdates: true)
let copied = UIGraphicsGetImageFromCurrentImageContext(); // You have to save this content in your internal memory
yourImageView.image = copied // To Preview that Image in your ImageView
UIGraphicsEndImageContext();

I am not sure about this but you can do so something like rendering HTML in the UIWebView. Then take the screenshot via code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!