[英]Convert 'Xamarin.Form.Image' type 'byte[]'
我應該從URL獲取圖像並將其轉換為byte[]
。 以下是我實現的
var webImage = new Image { Aspect = Aspect.AspectFit };
webImage.Source = ImageSource.FromUri(new Uri("http://xamarin.com/content/images/pages/forms/example-app.png"));
但是現在我無法將Image類型轉換為byte[]
。
這是正確的方法嗎? 任何幫助將不勝感激。
我還沒有嘗試過,但是從我最初的想法來看,您將需要一個Stream。 為此,您可以使用StreamImageSource
從那里,您可以使用this中的代碼。
其中的Xamarin.Forms使用IImageSourceHandler
,其實現如下
ImageLoaderSourceHandler
用於UriImageSource StreamImagesourceHandler
用於StreamImageSource 要從UriImageSource
讀取數據,應使用平台類ImageLoaderSourceHandler
。 所以在每個平台下使用類似
var handler = new ImageLoaderSourceHandler();
var uiimage = await handler.LoadImageAsync(source, new CancellationToken(), UIScreen.MainScreenScale);
然后,您可以從iOS下的UIImage或Android下的Bitmap獲取任何數據。 有很多教程如何從中獲取byte []。
FFImageLoading( https://github.com/molinch/FFImageLoading/ ) CachedImage
有GetImageAsJPG
和GetImageAsPNG
方法。 它是Xamarin.Forms Image
類的直接替代品,具有許多其他功能。
這是用於圖像檢索的代碼(可以在Image
類自定義渲染器中使用):
可以使用WebClient從URL獲取字節數組。
var webClient = new WebClient ();
byte[] byteImage = webClient.DownloadData (imageURL);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.