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

时间:2023-01-12 18:47:46

It has been 4 years since this question has 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.

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

4 个解决方案

#1


52  

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的调用,都是线程安全的。

#2


27  

With new await/async support you can do:

使用新的await / async支持,您可以:

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");

#3


5  

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 UrlImageStore out there to help with async loading images.

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

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

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

#4


1  

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 '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

谢谢

#1


52  

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的调用,都是线程安全的。

#2


27  

With new await/async support you can do:

使用新的await / async支持,您可以:

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");

#3


5  

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 UrlImageStore out there to help with async loading images.

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

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

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

#4


1  

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 '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

谢谢