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
/
Copy pathImageRenderer.cs
290 lines (245 loc) · 8.78 KB
/
ImageRenderer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Drawing;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Foundation;
using UIKit;
using Xamarin.Forms.Internals;
using RectangleF = CoreGraphics.CGRect;
using PreserveAttribute = Foundation.PreserveAttribute;
namespace Xamarin.Forms.Platform.iOS
{
public static class ImageExtensions
{
public static UIViewContentMode ToUIViewContentMode(this Aspect aspect)
{
switch (aspect)
{
case Aspect.AspectFill:
return UIViewContentMode.ScaleAspectFill;
case Aspect.Fill:
return UIViewContentMode.ScaleToFill;
case Aspect.AspectFit:
default:
return UIViewContentMode.ScaleAspectFit;
}
}
}
public class ImageRenderer : ViewRenderer<Image, FormsUIImageView>, IImageVisualElementRenderer
{
bool _isDisposed;
[Preserve(Conditional = true)]
public ImageRenderer() : base()
{
ImageElementManager.Init(this);
}
protected override void Dispose(bool disposing)
{
if (_isDisposed)
return;
if (disposing)
{
UIImage oldUIImage;
if (Control != null && (oldUIImage = Control.Image) != null)
{
ImageElementManager.Dispose(this);
oldUIImage.Dispose();
}
}
_isDisposed = true;
base.Dispose(disposing);
}
protected override async void OnElementChanged(ElementChangedEventArgs<Image> e)
{
if (e.NewElement != null)
{
if (Control == null)
{
var imageView = new FormsUIImageView();
imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
imageView.ClipsToBounds = true;
SetNativeControl(imageView);
}
await TrySetImage(e.OldElement as Image);
}
base.OnElementChanged(e);
}
protected override async void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Image.SourceProperty.PropertyName)
await TrySetImage().ConfigureAwait(false);
}
protected virtual async Task TrySetImage(Image previous = null)
{
// By default we'll just catch and log any exceptions thrown by SetImage so they don't bring down
// the application; a custom renderer can override this method and handle exceptions from
// SetImage differently if it wants to
try
{
await SetImage(previous).ConfigureAwait(false);
}
catch (Exception ex)
{
Log.Warning(nameof(ImageRenderer), "Error loading image: {0}", ex);
}
finally
{
((IImageController)Element)?.SetIsLoading(false);
}
}
protected async Task SetImage(Image oldElement = null)
{
await ImageElementManager.SetImage(this, Element, oldElement).ConfigureAwait(false);
}
void IImageVisualElementRenderer.SetImage(UIImage image) => Control.Image = image;
bool IImageVisualElementRenderer.IsDisposed => _isDisposed;
UIImageView IImageVisualElementRenderer.GetImage() => Control;
}
public interface IImageSourceHandler : IRegisterable
{
Task<UIImage> LoadImageAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1);
}
public interface IAnimationSourceHandler : IRegisterable
{
Task<FormsCAKeyFrameAnimation> LoadImageAnimationAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1);
}
public sealed class FileImageSourceHandler : IImageSourceHandler, IAnimationSourceHandler
{
[Preserve(Conditional = true)]
public FileImageSourceHandler()
{
}
public Task<UIImage> LoadImageAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1f)
{
UIImage image = null;
var filesource = imagesource as FileImageSource;
var file = filesource?.File;
if (!string.IsNullOrEmpty(file))
image = File.Exists(file) ? new UIImage(file) : UIImage.FromBundle(file);
if (image == null)
{
Log.Warning(nameof(FileImageSourceHandler), "Could not find image: {0}", imagesource);
}
return Task.FromResult(image);
}
public Task<FormsCAKeyFrameAnimation> LoadImageAnimationAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1)
{
FormsCAKeyFrameAnimation animation = ImageAnimationHelper.CreateAnimationFromFileImageSource(imagesource as FileImageSource);
if (animation == null)
{
Log.Warning(nameof(FileImageSourceHandler), "Could not find image: {0}", imagesource);
}
return Task.FromResult(animation);
}
}
public sealed class StreamImagesourceHandler : IImageSourceHandler, IAnimationSourceHandler
{
[Preserve(Conditional = true)]
public StreamImagesourceHandler()
{
}
public async Task<UIImage> LoadImageAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1f)
{
UIImage image = null;
var streamsource = imagesource as StreamImageSource;
if (streamsource?.Stream != null)
{
using (var streamImage = await ((IStreamImageSource)streamsource).GetStreamAsync(cancelationToken).ConfigureAwait(false))
{
if (streamImage != null)
image = UIImage.LoadFromData(NSData.FromStream(streamImage), scale);
}
}
if (image == null)
{
Log.Warning(nameof(StreamImagesourceHandler), "Could not load image: {0}", streamsource);
}
return image;
}
public async Task<FormsCAKeyFrameAnimation> LoadImageAnimationAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1)
{
FormsCAKeyFrameAnimation animation = await ImageAnimationHelper.CreateAnimationFromStreamImageSourceAsync(imagesource as StreamImageSource, cancelationToken).ConfigureAwait(false);
if (animation == null)
{
Log.Warning(nameof(FileImageSourceHandler), "Could not find image: {0}", imagesource);
}
return animation;
}
}
public sealed class ImageLoaderSourceHandler : IImageSourceHandler, IAnimationSourceHandler
{
[Preserve(Conditional = true)]
public ImageLoaderSourceHandler()
{
}
public async Task<UIImage> LoadImageAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1f)
{
UIImage image = null;
var imageLoader = imagesource as UriImageSource;
if (imageLoader?.Uri != null)
{
using (var streamImage = await imageLoader.GetStreamAsync(cancelationToken).ConfigureAwait(false))
{
if (streamImage != null)
image = UIImage.LoadFromData(NSData.FromStream(streamImage), scale);
}
}
if (image == null)
{
Log.Warning(nameof(ImageLoaderSourceHandler), "Could not load image: {0}", imageLoader);
}
return image;
}
public async Task<FormsCAKeyFrameAnimation> LoadImageAnimationAsync(ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1)
{
FormsCAKeyFrameAnimation animation = await ImageAnimationHelper.CreateAnimationFromUriImageSourceAsync(imagesource as UriImageSource, cancelationToken).ConfigureAwait(false);
if (animation == null)
{
Log.Warning(nameof(FileImageSourceHandler), "Could not find image: {0}", imagesource);
}
return animation;
}
}
public sealed class FontImageSourceHandler : IImageSourceHandler
{
readonly Color _defaultColor = ColorExtensions.LabelColor.ToColor();
[Preserve(Conditional = true)]
public FontImageSourceHandler()
{
}
public Task<UIImage> LoadImageAsync(
ImageSource imagesource,
CancellationToken cancelationToken = default(CancellationToken),
float scale = 1f)
{
UIImage image = null;
var fontsource = imagesource as FontImageSource;
if (fontsource != null)
{
//This will allow lookup from the Embedded Fonts
var cleansedname = FontExtensions.CleanseFontName(fontsource.FontFamily);
var font = UIFont.FromName(cleansedname ?? string.Empty, (float)fontsource.Size) ??
UIFont.SystemFontOfSize((float)fontsource.Size);
var iconcolor = fontsource.Color.IsDefault ? _defaultColor : fontsource.Color;
var attString = new NSAttributedString(fontsource.Glyph, font: font, foregroundColor: iconcolor.ToUIColor());
var imagesize = ((NSString)fontsource.Glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _));
UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f);
var ctx = new NSStringDrawingContext();
var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx);
attString.DrawString(new RectangleF(
imagesize.Width / 2 - boundingRect.Size.Width / 2,
imagesize.Height / 2 - boundingRect.Size.Height / 2,
imagesize.Width,
imagesize.Height));
image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
if (image != null && iconcolor != _defaultColor)
image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
}
return Task.FromResult(image);
}
}
}