0
私はハイブリッド(Cordova)モバイルアプリの開発者です.GIF画像をさまざまなソーシャルメディアプラットフォームに共有することが条件です。私は自分の画像をBase64データのURLに変換する関数を書いています。主に画像を変換してシェアをスムーズにしますが、シェアボタンのクリックで画像を共有できないことがあります。また、共有ウィンドウが開かないこともあります。画像を変換するのに少し時間がかかると思われます。ここに私のサンプルコードです:JavaScriptを使用してGIF画像をBase64データURIに変換する際のパフォーマンスの問題
function convertFileToDataURLviaFileReader(url, callback) {
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result);
}
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.send();
}
そして、これは、関数が呼び出されている方法です。
//The below function will be called on click of share buttons
function showSnackBar(e) {
imgSource = null;
selectedImgId = null;
imgDataURL = null;
var x = document.getElementById("snackbar");
imgSource = e.target.currentSrc;
selectedImgId = e.target.id;
x.className = "show";
setTimeout(function() {
x.className = x.className.replace("show", "");
}, 3000);
//calling function to Convert ImageURL to DataURL
convertFileToDataURLviaFileReader(imgSource, function (base64Img) {
imgDataURL = base64Img;
});
$("#btnShare").click(function (e) {
if(imgDataURL != null) {
window.plugins.socialsharing.share(null, 'Android filename', imgDataURL, null)
}
})
};
これは、複数の共有ウィンドウ@femiを開いています。ウィンドウカウントは1,2,3,4から順に増加します。 –
また、実際にイメージを変換する必要がありますか?元の画像ソースをそのまま使用すればいいですか? –