From 11f29bca86e299ed3e2c0d3db0d8058efe4be7ef Mon Sep 17 00:00:00 2001 From: Liam Murphy <liampm32@gmail.com> Date: Sat, 10 Apr 2021 17:18:04 +1000 Subject: Use data urls instead of blob URLs I didn't do this originally because I was half doing it in the first place to mess with Blob URLs, and it feels kinda wrong to be encoding it as base64 when that option is available. But not having memory leaks is more important. --- web/Cargo.toml | 4 +--- web/src/widget/image.rs | 21 ++++++--------------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/web/Cargo.toml b/web/Cargo.toml index d6bc34da..f8630780 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -20,7 +20,7 @@ wasm-bindgen = "0.2" wasm-bindgen-futures = "0.4" url = "2.0" num-traits = "0.2" -js-sys = "0.3" +base64 = "0.13" [dependencies.iced_core] version = "0.3" @@ -45,6 +45,4 @@ features = [ "EventTarget", "InputEvent", "KeyboardEvent", - "Url", - "Blob", ] diff --git a/web/src/widget/image.rs b/web/src/widget/image.rs index f38f5cfb..28435f4f 100644 --- a/web/src/widget/image.rs +++ b/web/src/widget/image.rs @@ -2,15 +2,11 @@ use crate::{Bus, Css, Element, Hasher, Length, Widget}; use dodrio::bumpalo; -use js_sys::Array; -use js_sys::Uint8Array; use std::{ hash::{Hash, Hasher as _}, path::PathBuf, sync::Arc, }; -use web_sys::Blob; -use web_sys::Url; /// A frame that displays an image while keeping aspect ratio. /// @@ -79,18 +75,13 @@ impl<Message> Widget<Message> for Image { let src = match self.handle.data.as_ref() { Data::Path(path) => { String::from_str_in(path.to_str().unwrap_or(""), bump) - .into_bump_str() } - Data::Bytes(bytes) => bump.alloc( - Url::create_object_url_with_blob( - &Blob::new_with_u8_array_sequence(&Array::of1( - &Uint8Array::from(bytes.as_slice()), - )) - .unwrap(), - ) - .unwrap(), - ), - }; + Data::Bytes(bytes) => { + // The web is able to infer the kind of image, so we don't have to add a dependency on image-rs to guess the mime type. + bumpalo::format!(in bump, "data:;base64,{}", base64::encode(bytes)) + }, + } + .into_bump_str(); let alt = String::from_str_in(&self.alt, bump).into_bump_str(); -- cgit