I tried to load a .heic
image using WKWebView.loadFileURL
on iOS 11.4 but got a blank page.
(I am sure the url is right as I can preview it in xcode)
Asked
Active
Viewed 775 times
3

John Smith
- 153
- 1
- 7
-
After went home and slept for a night and come back, I found it work properly with nothing changed. – John Smith Aug 10 '18 at 02:59
-
It seems that OneDrive has converted .heic file to jpeg format automacitcally, still unable to show .heic files – John Smith Aug 10 '18 at 10:01
-
I had the same problem. – szotp Sep 24 '19 at 07:42
1 Answers
0
Convert "heic" image to jpeg data and load with the help of html as:
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)
}

Ankit Jayaswal
- 5,549
- 3
- 16
- 36