簡體   English   中英

如何上傳從照片庫圖像或相機中的WebView中的iOS

[英]How to upload image from Photo Gallery or Camera in WebView in iOS

我的應用程序有一個WebView。 在這種WebView中,我想從照片庫或照相機上傳圖片。

我用WebChromeClient做到了為Android。 我不知道如何使用Objective-C做iOS中。

當用戶點擊webview上的上載圖片按鈕時,會從webview發送ScriptMessage到iOS,並在iOS中在WebKit WKScriptMessageHandler中捕獲該消息,

userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)

這里的messagemessage.body包含來自webview的腳本消息。 然后根據消息打開相機或圖庫,並在imagepickerDelegate照片作為本機應用程序保存到適當的API。 保存后,您也可以刷新Web視圖。

是一個SO帖子,提供更詳盡的答案

假設您的上傳代碼在JavaScript中,則可以將以下內容添加到HTML中

<label for="fileupload">Upload</label>
<input id="fileupload" accept="image/*" style="display:none;" type="file">

在js中將更改事件添加到輸入字段,然后上傳文件。 這是JQuery中的代碼

$("#fileupload").on("change", function(value){
    var value = $("#fileupload").get(0).files[0]
    // clear file so if file is loaded a second time the change event is still triggered
    $("#fileupload").val('')
    // your code to upload the file
    uploadFile(value)
})

您只需要做的其他事情就是將“隱私-相機使用說明”和“隱私-照片庫使用說明”標志添加到應用程序Info.plist文件中,以使您的應用程序可以訪問相機和照相館。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM