HTML內容示例:This is HTML Content.<div>This is <b>BOLD</b> text </div><div>This is <i> ITALIC </i> text</div>
這是要呈現的HTML內容並將其放置在UIImageView中,然後保存此圖像。 我試圖先渲染HTML內容並生成一個PDF,然後將該PDF轉換爲Image,但該圖像效果很差。所以這就是爲什麼我找到一些呈現我的HTML內容並添加到UIImageVIew中的東西,並且我會輕鬆地將該圖像保存在我的應用程序內存中。將HTML內容轉換爲圖像
1
A
回答
1
使用標籤嘗試此解決方案/ UITextView
將html文本設置爲您的標籤。
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)
}
執行快照試試下面的代碼
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();
+1
完成這件事。但是使用「UITextView」而不是「LABEL」,因爲labelview不會渲染圖像,背景也會發生變化。 –
1
我不確定這一點,但你可以這樣做,就像在UIWebView呈現HTML。然後通過代碼截圖。
+0
我已經完成了。但使用這種方法的圖像質量很差。 –
+0
怎麼樣在UITextView中顯示它作爲NSAttributedstring然後截圖。 (正如上面的卡迪爾所說) –
相關問題
- 1. 將HTML Div內容轉換爲圖像
- 2. 將IFRAME內容轉換爲圖像
- 3. 將div的內容轉換爲圖像
- 4. 將圖像轉換爲背景內容
- 5. PHP:將blob內容轉換爲圖像
- 6. 將html圖像轉換爲內聯base64
- 7. 將HTML轉換爲圖像
- 8. 使用javascript將html內容轉換爲圖像
- 9. 將textarea內容轉換爲html
- 10. 將XML內容轉換爲HTML
- 11. 將MS Word內容轉換爲HTML
- 12. 如何將html內容轉換爲pdf?
- 13. 將HTML內容轉換爲Javascript代碼
- 14. 如何將html內容轉換爲wordpress
- 15. 將HTML內容轉換爲列表
- 16. 將html視圖轉換爲圖像
- 17. 使用遠程圖像和樣式表將HTML HTML內容轉換爲DOC/PDF
- 18. 將div和其內的內容轉換爲圖像
- 19. 將DIV從html轉換爲圖像
- 20. 將html轉換爲ios中的圖像
- 21. 將HTML畫布轉換爲圖像。 Javascript
- 22. 將XSL轉換爲HTML - 插入圖像
- 23. 將圖像轉換爲html部分
- 24. 將HTML字符串轉換爲圖像
- 25. 將HTML錶轉換爲圖像文件
- 26. 將HTML輸出轉換爲圖像
- 27. 將.html文件轉換爲圖像
- 28. 將HTML DOM轉換爲圖像
- 29. 將html輸出轉換爲圖像
- 30. 將html轉換爲圖像的程序
要'UIImageView'請解釋......具體到斯威夫特 –
的UIImageView。 –
你想把這個內容放到UIImageView中嗎?似乎更多的邏輯把它放在一個UITextView或UILabel ... –