diff --git a/vnext/ReactUWP/Views/Image/ReactImage.cpp b/vnext/ReactUWP/Views/Image/ReactImage.cpp index f52e90b810c..9555c4c78bb 100644 --- a/vnext/ReactUWP/Views/Image/ReactImage.cpp +++ b/vnext/ReactUWP/Views/Image/ReactImage.cpp @@ -6,6 +6,7 @@ #include "ReactImage.h" #include +#include #include "unicode.h" @@ -75,6 +76,7 @@ namespace react { winrt::Uri uri{ facebook::utf8ToUtf16(uriString) }; winrt::hstring scheme{ uri.SchemeName() }; bool needsDownload = (scheme == L"http") || (scheme == L"https"); + bool inlineData = scheme == L"data"; try { @@ -84,16 +86,20 @@ namespace react { if (needsDownload) { memoryStream = co_await GetImageStreamAsync(source); - - if (!memoryStream) - { - m_onLoadEndEvent(*this, false); - } + } + else if (inlineData) + { + memoryStream = co_await GetImageInlineDataAsync(source); } + if ((needsDownload || inlineData) && !memoryStream) + { + m_onLoadEndEvent(*this, false); + } + if (!needsDownload || memoryStream) { - auto surface = needsDownload ? + auto surface = needsDownload || inlineData ? winrt::LoadedImageSurface::StartLoadFromStream(memoryStream) : winrt::LoadedImageSurface::StartLoadFromUri(uri); @@ -166,5 +172,32 @@ namespace react { return nullptr; } + + winrt::IAsyncOperation GetImageInlineDataAsync(ImageSource source) + { + size_t start = source.uri.find(','); + if (start == std::string::npos || start + 1 > source.uri.length()) + return nullptr; + + try + { + co_await winrt::resume_background(); + + std::string_view base64String(source.uri.c_str() + start + 1, source.uri.length() - start - 1); + auto buffer = winrt::Windows::Security::Cryptography::CryptographicBuffer::DecodeFromBase64String(facebook::react::unicode::utf8ToUtf16(base64String)); + + winrt::InMemoryRandomAccessStream memoryStream; + co_await memoryStream.WriteAsync(buffer); + memoryStream.Seek(0); + + return memoryStream; + } + catch (winrt::hresult_error const&) + { + // Base64 decode failed + } + + return nullptr; + } } } // namespace react::uwp diff --git a/vnext/ReactUWP/Views/Image/ReactImage.h b/vnext/ReactUWP/Views/Image/ReactImage.h index 61213da6782..e1711ee251e 100644 --- a/vnext/ReactUWP/Views/Image/ReactImage.h +++ b/vnext/ReactUWP/Views/Image/ReactImage.h @@ -62,5 +62,6 @@ namespace react { // Helper functions winrt::Windows::Foundation::IAsyncOperation GetImageStreamAsync(ImageSource source); + winrt::Windows::Foundation::IAsyncOperation GetImageInlineDataAsync(ImageSource source); } } // namespace react::uwp diff --git a/vnext/Universal.SampleApp/index.uwp.js b/vnext/Universal.SampleApp/index.uwp.js index 9676460384a..04596d9bcd9 100644 --- a/vnext/Universal.SampleApp/index.uwp.js +++ b/vnext/Universal.SampleApp/index.uwp.js @@ -262,6 +262,15 @@ export default class Bootstrap extends Component { onError={() => { console.log('image onError!'); }} /> + + + this.setState({ checkBoxIsOn: value })}