iOS Question load image center in wkwebview

ilan

Expert
Licensed User
Longtime User
i am not sure if it is a bug but i get very strange behavior.

i have a wkwebview and i am loading an image to it (and also other files)
now the thing is when i start the app and try to load the image using loadhtml() it does not load it BUT if i load before that another file or the same file with the SAME path with loadurl() it does load and then if i use loadhtml() it works.

so ONLY if the loadhtm() was first it does not work but if loadulr() came before loadhtml() it does work.

this happens only if i use file.dirdocument. from file.dirassets it works fine.

the info of Powerview says the the path is relative to file.dirassets folder but can i change it so it works also with file.dirdocuments
 

ilan

Expert
Licensed User
Longtime User

thanx for you answer erel. i saw it and i also use it. but using

B4X:
WV.LoadUrl("file://" & File.Combine(Dir, FileName))

does load the image on top and i want it to be centred so i use this function:

B4X:
Private Sub loadFileToWebView(WV As WKWebView, Dir As String, FileName As String, isImage As Boolean) 'ignore
    If isImage Then
       ' WV.LoadUrl(xui.FileUri(Dir, FileName))
        WV.LoadHtml("<html><head></head><body><div class=" & """" & "container" & """" & "> <img src= """ & "file://" & xui.FileUri(Dir, FileName) &  """/><style>.container {width: 100%;height: 100%;position: absolute;margin:auto;top:0;bottom:0;left:0;right:0;background-color: #fff;}img {max-width: 100%;max-height: 100%; position: absolute;margin:auto;top:0;bottom:0;left:0;right:0;}<style></body></html>")
    Else
        WV.LoadUrl(xui.FileUri(Dir, FileName))
    End If
End Sub

now to load the image on first start i have to load it first with loadurl and then use loadhtml. if i use loadhtml without loadurl it wort load the image.
the weired thing is that if i load another file like pdf with loadurl and then use loadhtml it works.

why is it like that?
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I tested your code under IOS 13 simulator (I took simple name "1.jpg"). Works fine for File.DirDocuments.
This is a surprise, because file://" & xui.FileUri(Dir, FileName) is incorrect. xui.FileUri already includes file:///
 
Upvote 0
Top