Skip to content

Commit 51c6e2a

Browse files
authored
fix(ImageLoader): getSize API behavior for local assets (microsoft#935)
Recent changes to the ReactImageManager and the ImageLoaderModule introduced a bug where local assets were not behaving correctly because they did not work with the ImageCache API from UWP Community Toolkit. This fix works around that limitation and uses the previous `getSize` implementation approach for non-HTTP assets. Fixes microsoft#930
1 parent 9b7d65e commit 51c6e2a

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

ReactWindows/ReactNative/Modules/Image/ImageLoaderModule.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Newtonsoft.Json.Linq;
33
using ReactNative.Bridge;
44
using System;
5+
using System.Reactive.Linq;
6+
using Windows.UI.Xaml.Media.Imaging;
57

68
namespace ReactNative.Modules.Image
79
{
@@ -57,12 +59,39 @@ public void getSize(string uriString, IPromise promise)
5759
{
5860
try
5961
{
60-
var bitmapImage = await ImageCache.Instance.GetFromCacheAsync(new Uri(uriString), true);
61-
promise.Resolve(new JObject
62+
if (BitmapImageHelpers.IsHttpUri(uriString))
6263
{
63-
{ "width", bitmapImage.PixelWidth },
64-
{ "height", bitmapImage.PixelHeight },
65-
});
64+
var bitmapImage = await ImageCache.Instance.GetFromCacheAsync(new Uri(uriString), true);
65+
promise.Resolve(new JObject
66+
{
67+
{ "width", bitmapImage.PixelWidth },
68+
{ "height", bitmapImage.PixelHeight },
69+
});
70+
}
71+
else
72+
{
73+
var bitmapImage = new BitmapImage();
74+
var loadQuery = bitmapImage.GetStreamLoadObservable()
75+
.Where(status => status.LoadStatus == ImageLoadStatus.OnLoadEnd)
76+
.FirstAsync()
77+
.Replay(1);
78+
79+
using (loadQuery.Connect())
80+
{
81+
using (var stream = await BitmapImageHelpers.GetStreamAsync(uriString))
82+
{
83+
await bitmapImage.SetSourceAsync(stream);
84+
}
85+
86+
await loadQuery;
87+
88+
promise.Resolve(new JObject
89+
{
90+
{ "width", bitmapImage.PixelWidth },
91+
{ "height", bitmapImage.PixelHeight },
92+
});
93+
}
94+
}
6695
}
6796
catch (Exception ex)
6897
{

0 commit comments

Comments
 (0)