微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将对本地图像的引用插入html并在swift标签中显示出来?

如何解决如何将对本地图像的引用插入html并在swift标签中显示出来?

在资产文件夹中,我有en文件夹,这里有一些图片,例如其中一张-B12.png。我不明白如何在标签显示此本地图片我有这样的代码

let imageData:Data =  (UIImage.init(named: "en/B12.png")!).pngData()!
let base64String = imageData.base64EncodedString()

                            
let htmlString = """
<html><body><img src="data:image/png;base64,\(base64String)" /> width=\"360\" height=\"240\"></body></html>
"""
                            
                            
cell.questionText.attributedText = htmlString.convertToAttributedFromHTML()

但是当我运行这段代码时,我的标签上什么都没有看到。为什么会发生,如何解决这个问题?

解决方法

如果图像存储在应用程序的捆绑包中,则只需插入图像的URL

if let imagePath = Bundle.main.path(forResource: "B12",ofType: "png") {
    let htmlString = """
    <html><body><img src="\(imagePath)" /> width=\"360\" height=\"240\"></body></html>
    """

    cell.questionText.attributedText = htmlString.convertToAttributedFromHTML()
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。