I have a Xamarin.Forms project which uses the native photo choosers on Android and iOS to load an image from the gallery or the camera and display it in a Forms page.
My implementation uses the DependencyService to get the actual photos, and creates a StreamImageSource which I send asynchronously to my Forms page via the MessagingCenter.
I use that ImageSource to show thumbnails in on a page by creating a new Image() and setting Image.Source to that source.. When those thumbnails are tapped, I show a larger image, by creating a larger Image() and setting the same Image.Source to it.
On Android, I have no problems::
var imgsrc = ImageSource.FromStream(() =>
{
MemoryStream ms = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 100, ms);
ms.Seek(0L, SeekOrigin.Begin);
return ms;
});
MessagingCenter.Send<Object, ImageSource>(this, Constants.MsgPhotoData, imgsrc);
on iOS I do the following:
UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
if(originalImage != null)
{
ImageSource imgsrc = ImageSource.FromStream( () =>
{
return originalImage .AsPNG().AsStream();
} );
}
MessagingCenter.Send<Object, ImageSource>(this, Constants.MsgPhotoData, imgsrc);
In Forms, in m MessagingCenter.Subscribe delegate I do something like this:
var thumbnailImage= new Image();
thumbnailImage.Source = arg as ImageSource,
Later on, if that thumbnail is tapped, I show a larger image by:
var fullSizeImage = new Image();
fullSizeImage.WidthRequest = fullsizeImageWidth;
fullSizeImage.HeightRequest = fullsizeImageHeight;
fullSizeImage.Source = thumbnailImage.Source;
So on iOS, this only works if I don't dispose 'originalImage', because setting fullSizeImage.Source to thumbnailImage.Source calls the
ImageSource imgsrc = ImageSource.FromStream( () =>
{
return originalImage .AsPNG().AsStream();
} );
code again.
This is not what I want to do.
The ImageSource has already read the stream. Why does it try to re-read it every time I access it? Is there a way to cache it?