This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ImageViewExtensions.cs
68 lines (55 loc) · 1.86 KB
/
ImageViewExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Threading.Tasks;
using Android.Graphics;
using AImageView = Android.Widget.ImageView;
namespace Xamarin.Forms.Platform.Android
{
internal static class ImageViewExtensions
{
// TODO hartez 2017/04/07 09:33:03 Review this again, not sure it's handling the transition from previousImage to 'null' newImage correctly
public static async Task UpdateBitmap(this AImageView imageView, Image newImage, Image previousImage = null)
{
if (imageView == null || imageView.IsDisposed())
return;
if (Device.IsInvokeRequired)
throw new InvalidOperationException("Image Bitmap must not be updated from background thread");
if (previousImage != null && Equals(previousImage.Source, newImage.Source))
return;
var imageController = newImage as IImageController;
imageController?.SetIsLoading(true);
(imageView as IImageRendererController)?.SkipInvalidate();
imageView.SetImageResource(global::Android.Resource.Color.Transparent);
ImageSource source = newImage?.Source;
Bitmap bitmap = null;
IImageSourceHandler handler;
if (source != null && (handler = Internals.Registrar.Registered.GetHandlerForObject<IImageSourceHandler>(source)) != null)
{
try
{
bitmap = await handler.LoadImageAsync(source, imageView.Context);
}
catch (TaskCanceledException)
{
imageController?.SetIsLoading(false);
}
}
if (newImage == null || !Equals(newImage.Source, source))
{
bitmap?.Dispose();
return;
}
if (!imageView.IsDisposed())
{
if (bitmap == null && source is FileImageSource)
imageView.SetImageResource(ResourceManager.GetDrawableByName(((FileImageSource)source).File));
else
{
imageView.SetImageBitmap(bitmap);
}
}
bitmap?.Dispose();
imageController?.SetIsLoading(false);
((IVisualElementController)newImage).NativeSizeChanged();
}
}
}