从 Xamarin / C# 中的 URL 加载的 UIImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17821371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:34:44  来源:igfitidea点击:

UIImage loaded from URL in Xamarin / C#

c#iosxamarin.iosuiimagexamarin

提问by Jason Hartley

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

自从这篇博文回答这个问题以来,已经过去了 4 年。

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

是否有标准方法可以使用来自 URL 的图像创建 UIImage?就像是:

UIImage image = UIImage.FromFile("http://foo.com/bar.jpg");

I feel like I'm probably missing something really simple.

我觉得我可能错过了一些非常简单的东西。

采纳答案by poupou

Not a one-liner, but with very few lines you can roll your own. E.g.

不是单行,但你可以用很少的行来滚动你自己的行。例如

static UIImage FromUrl (string uri)
{
    using (var url = new NSUrl (uri))
    using (var data = NSData.FromUrl (url))
        return UIImage.LoadFromData (data);
}

The calls, including the one from UIImage, are thread-safe.

调用(包括来自 的调用)UIImage是线程安全的。

回答by Jason

You want to be sure that you load the image async so that you do not block your UI thread. MonoTouch.Dialog includes an ImageLoader(see sec 5.3) class that you could use.

您希望确保异步加载图像,以免阻塞 UI 线程。MonoTouch.Dialog 包含一个您可以使用的ImageLoader(参见第 5.3 节)类。

There are also a couple of variations of UrlImageStoreout there to help with async loading images.

还有一些UrlImageStore的变体可以帮助异步加载图像。

Finally, if you want to do it manually, there is a Xamarin Recipeyou can use.

最后,如果您想手动完成,您可以使用Xamarin Recipe

回答by Pavel Sich

With new await/async support you can do:

有了新的等待/异步支持,您可以:

public async Task<UIImage> LoadImage (string imageUrl)
        {
            var httpClient = new HttpClient();

            Task<byte[]> 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");

回答by Scott Emick

I tried the above, it looks like a great idea, but I get: Cannot implicitly convert type System.Threading.Tasks.Task<MonoTouch.UIKit.UIImage>' toMonotouch.UIKit.UIImage'

我尝试了上面的方法,看起来是个好主意,但我得到:无法隐式转换类型System.Threading.Tasks.Task<MonoTouch.UIKit.UIImage>' toMonotouch.UIKit.UIImage'

[found a solution] The problem was because the
obj.Image = await this.LoadImage (imageUrl) must also be in a method marked async. Then it works!

[找到解决方案] 问题是因为
obj.Image = await this.LoadImage (imageUrl) 也必须在标记为 async 的方法中。然后它起作用了!

Thanks

谢谢

回答by Anisetti Nagendra

Below code should work,

下面的代码应该工作,

public static async Task<UIImage> LoadImage(string imageUrl)
{
            var httpClient = new HttpClient();

            var contents = await httpClient.GetByteArrayAsync(imageUrl);
            return UIImage.LoadFromData(NSData.FromArray(contents));
}