最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - Trouble sending base64 image from WKWebView to JavaScript on page - Stack Overflow

programmeradmin2浏览0评论

I'm trying to send a Base64 representation of a UIImage in iOS to a Web View and I think I'm running into an error where the string is being escaped in JavaScript because of special characters, but I'm not quite sure how to handle that. This is what I have to far...

func imagePickerController(_ picker: UIImagePickerController,
                           didFinishPickingMediaWithInfo info: [String : Any])
{
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    let thumb = chosenImage.resized(toWidth: 72.0)

    let imageData:NSData = UIImagePNGRepresentation(thumb!)! as NSData

    let dataImage:String = imageData.base64EncodedString(options: .lineLength64Characters)

    webView?.evaluateJavaScript("window.settings.setImageBase64FromiOS('\(dataImage)');") { (result, error) in
        if error != nil {
            print(error!)
        } else {
            print("Success")
        }
    }
    dismiss(animated:true, pletion: nil) //5

}

I keep getting this error logged to the console.

WKJavaScriptExceptionMessage=SyntaxError: Unexpected EOF

I'm trying to send a Base64 representation of a UIImage in iOS to a Web View and I think I'm running into an error where the string is being escaped in JavaScript because of special characters, but I'm not quite sure how to handle that. This is what I have to far...

func imagePickerController(_ picker: UIImagePickerController,
                           didFinishPickingMediaWithInfo info: [String : Any])
{
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    let thumb = chosenImage.resized(toWidth: 72.0)

    let imageData:NSData = UIImagePNGRepresentation(thumb!)! as NSData

    let dataImage:String = imageData.base64EncodedString(options: .lineLength64Characters)

    webView?.evaluateJavaScript("window.settings.setImageBase64FromiOS('\(dataImage)');") { (result, error) in
        if error != nil {
            print(error!)
        } else {
            print("Success")
        }
    }
    dismiss(animated:true, pletion: nil) //5

}

I keep getting this error logged to the console.

WKJavaScriptExceptionMessage=SyntaxError: Unexpected EOF

Share Improve this question asked Oct 22, 2017 at 16:47 Miguel CoderMiguel Coder 1,94720 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I also encountered the same problem and I tried encoding the string before sending to javascript as follows:

let encodedString: String = dataImage.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? ""

In Javascript code, before adding to image source,

var originalImageData = decodeURIComponent(encodedImageDataString)

P.S: hope you have the final image data with prefix in this format data:;base64, for eg.

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAIAAACx0UUtAAAAAXNSR0IArs4c6QAA...

I encountered the same problem but managed to get it to work by setting the base64EncodedStringWithOptions parameter to 0 instead of 'lineLength64Characters'. I think the line breaks were the problem, '0' sets it to no line breaks.

发布评论

评论列表(0)

  1. 暂无评论