0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ReactNativeでWebView内の画像をbase64に変換して取得する方法

Last updated at Posted at 2025-04-06

はじめに

今回は、ReactNativeでWebView内の画像をbase64に変換して取得する方法について紹介していきます。

結論のコード

// 画像URLからblobを取得し、Base64に変換する関数
function convertImageToBase64(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.onload = function() {
    var reader = new FileReader();
    reader.onloadend = function() {
      callback(reader.result);
    };
    reader.readAsDataURL(xhr.response);
  };
  xhr.onerror = function(err) {
    callback("error fetching image");
  };
  xhr.open('GET', url);
  xhr.responseType = 'blob';
  xhr.send();
}

こちらをinjectedJavaScriptに挿入して使用する感じになります。

本来はfetchやcanvasを使用すべきだと思いますが、なぜかそれだと弾かれてしまったので最終的に行き着いたのが今回のコードです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?