UIImage loaded from URL in Xamarin / C#

前端 未结 5 999
情歌与酒
情歌与酒 2021-02-01 17:07

It has been 4 years since this question has been answered with this blog post.

Is there a standard way to create a UIImage with an image from a URL? Something like:

5条回答
  •  佛祖请我去吃肉
    2021-02-01 17:59

    With new await/async support you can do:

    public async Task LoadImage (string imageUrl)
            {
                var httpClient = new HttpClient();
    
                Task contentsTask = httpClient.GetByteArrayAsync (imageUrl);
    
                // await! control returns to the caller and the task continues to run on another thread
                var contents = await contentsTask;
    
                // load from bytes
                return UIImage.LoadFromData (NSData.FromArray (contents));
            }
    

    and you call this with:

    someYourUIImageObjectOnUI.Image = await this.LoadImage ("some image url");
    

提交回复
热议问题