3

我尝试在 iOS 11.4 上加载.heic图像WKWebView.loadFileURL,但得到一个空白页。(我确定网址是正确的,因为我可以在 xcode 中预览它)

4

1 回答 1

0

将“heic”图像转换为 jpeg 数据并在 html 的帮助下加载为:

if fileExtension == "heic" {

    guard let fileData = fileData, 
        let image = UIImage(data: fileData), 
        let jpegData = image.jpegData(compressionQuality: 1.0) 
    else { 
        return 
    }

    let base64JPG = jpegData.base64EncodedString(options: .lineLength64Characters)
    let htmlImgTag = "<img src=\"data:image/jpeg;base64, " + base64JPG + "\" alt=\"" + fileUrl.lastPathComponent + "\" />"

    let imageHTML = "<!DOCTYPE html>" +
                    "<html lang=\"ja\">" +
                    "<head>" +
                    "<meta charset=\"UTF-8\">" +
                    "<style type=\"text/css\">" +
                    "html{margin:0;padding:0;}" +
                    "body {" +
                    "margin: 0;" +
                    "padding: 0;" +
                    "color: #363636;" +
                    "font-size: 90%;" +
                    "line-height: 1.6;" +
                    "background: black;" +
                    "}" +
                    "img{" +
                    "position: absolute;" +
                    "top: 0;" +
                    "bottom: 0;" +
                    "left: 0;" +
                    "right: 0;" +
                    "margin: auto;" +
                    "max-width: 100%;" +
                    "max-height: 100%;" +
                    "}" +
                    "</style>" +
                    "</head>" +
                    "<body id=\"page\">" +
                    "\(htmlImgTag)" +
                    "</body></html>"

    webView.loadHTMLString(imageHTML, baseURL: nil)
}
于 2019-09-25T12:56:08.397 回答