简体   繁体   中英

“Creating an image format with an unknown type is an error” when displaying a WebApp through WKWebView

I am displaying a WebApp through WKWebView in Swift 3 (Xcode 8.3.3)

The WebApp is functioning as expected, except when I try to post an image to the server through the WebView.

The console from Xcode simply prints "Creating an image format with an unknown type is an error" without any further info to help me troubleshoot.

As this thread indicates , there might be something with alerts that is causing this issue. That the WebView is not set up correctly to display alerts and that this might cause an error.

However I have not been able to figure that one out by looking at the suggested solutions there.

In my viewWillAppear I call the function initWebView() which is set up like this:

func initWebView() {
   let source: NSString = "var meta = document.createElement('meta');" +
        "meta.name = 'viewport';" +
        "meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +
        "var head = document.getElementsByTagName('head')[0];" +
        "head.appendChild(meta);" as NSString
    let sourceScript: WKUserScript = WKUserScript(source: source as String, injectionTime: .atDocumentStart, forMainFrameOnly: true)

    let addCookieScript="localStorage.setItem('device', '\(self.tokenFirebase)');"
    let script: WKUserScript = WKUserScript(source: addCookieScript as String, injectionTime: .atDocumentStart, forMainFrameOnly: false)

    // Create the user content controller and add the script to it
    let userContentController = WKUserContentController()
    userContentController.addUserScript(script)
    userContentController.addUserScript(sourceScript)

    // Create the configuration with the user content controller
    let config = WKWebViewConfiguration()
    config.userContentController = userContentController

    let wkWebView = WKWebView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), configuration: config)
    wkWebView.translatesAutoresizingMaskIntoConstraints = true
    self.wkWebView = wkWebView
    wkWebView.restorationIdentifier = "wkWebView"
    view.addSubview(wkWebView)
    wkWebView.navigationDelegate = self
    wkWebView.uiDelegate = self
    let request : NSMutableURLRequest = NSMutableURLRequest(url: self.webUrl as URL)
    wkWebView.isHidden = false
    wkWebView.load(request as URLRequest)

}

I have been testing the app with the same results across iPhone SE (iOS 10) iPhone 7 (iOS 10) and iPad (iOS 9.3.3).

EDIT: The possible duplicate entry does not appear valid to me as it refers to implementing UIImagePickerController by code, but in my scenario it is the image picker that is created by the WKWebView which seems to cause this issue. However if I'm mistaken please guide me in the right direction :)

Creating an image format with an unknown type is an error this is a generic warning you can safely ignore the warning. The problem should be something else.

import UIKit
import WebKit
class ViewController: UIViewController {
    var webView:WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
       loadWebView()
    }

    func loadWebView(){
        webView = WKWebView(frame: self.view.frame)
        self.view.addSubview(webView)
        let urlRequest = URLRequest(url: URL(string: "https://imgbb.com/")!)
        webView.load(urlRequest)
    }
}

I created a webview and tried to upload an image from photo library. it does give me that warning but it successfully uploaded the pic from photo library. You can try to above code and see if it works for you. btw don't forget to add permission explanation to access photolibrary in info.plist

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM