diff options
author | 2021-09-01 13:41:59 +0700 | |
---|---|---|
committer | 2021-09-01 13:41:59 +0700 | |
commit | dcd362813af45efbde84db940cfe6f6fbf40ca7b (patch) | |
tree | 84f7fd5158738c3984b8631e068594901d774cf7 /web/src/widget | |
parent | 4aa741af17ce9edec921be4a40b16301274289be (diff) | |
parent | 11f29bca86e299ed3e2c0d3db0d8058efe4be7ef (diff) | |
download | iced-dcd362813af45efbde84db940cfe6f6fbf40ca7b.tar.gz iced-dcd362813af45efbde84db940cfe6f6fbf40ca7b.tar.bz2 iced-dcd362813af45efbde84db940cfe6f6fbf40ca7b.zip |
Merge pull request #788 from Liamolucko/web-in-memory-image
feat(web): Support in-memory image data
Diffstat (limited to 'web/src/widget')
-rw-r--r-- | web/src/widget/image.rs | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/web/src/widget/image.rs b/web/src/widget/image.rs index 05c89ea5..28435f4f 100644 --- a/web/src/widget/image.rs +++ b/web/src/widget/image.rs @@ -72,12 +72,15 @@ impl<Message> Widget<Message> for Image { use dodrio::builder::*; use dodrio::bumpalo::collections::String; - let src = String::from_str_in( - match self.handle.data.as_ref() { - Data::Path(path) => path.to_str().unwrap_or(""), + let src = match self.handle.data.as_ref() { + Data::Path(path) => { + String::from_str_in(path.to_str().unwrap_or(""), bump) + } + 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)) }, - bump, - ) + } .into_bump_str(); let alt = String::from_str_in(&self.alt, bump).into_bump_str(); @@ -122,6 +125,14 @@ impl Handle { Self::from_data(Data::Path(path.into())) } + /// Creates an image [`Handle`] containing the image data directly. + /// + /// This is useful if you already have your image loaded in-memory, maybe + /// because you downloaded or generated it procedurally. + pub fn from_memory(bytes: Vec<u8>) -> Handle { + Self::from_data(Data::Bytes(bytes)) + } + fn from_data(data: Data) -> Handle { let mut hasher = Hasher::default(); data.hash(&mut hasher); @@ -160,12 +171,16 @@ impl From<&str> for Handle { pub enum Data { /// A remote image Path(PathBuf), + + /// In-memory data + Bytes(Vec<u8>), } impl std::fmt::Debug for Data { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Data::Path(path) => write!(f, "Path({:?})", path), + Data::Bytes(_) => write!(f, "Bytes(...)"), } } } |