【问题标题】:What is the best way to download image from url in Xamarin ios?从 Xamarin ios 中的 url 下载图像的最佳方法是什么?
【发布时间】:2018-03-24 14:22:46
【问题描述】:

我需要为每次平移和捏缩放下载打开街道地图图块。我使用 WebClient 从 Uri 下载图像。但我收到“一个 HttpWebRequest 已添加到连接组队列,因为已达到连接 linit,很快,我在 Xamarin iOS 中收到 System.Net.WebException。但我在 Xamarin.Android 中也是如此,其中的图像下载比 Xamarin iOS 好。

        WebClient webClient = new WebClient();
        byte[] imageBytes = null;
        Uri uri = new Uri("http://tile.openstreetmap.org/" + Scale.ToString() + "/" + i.ToString() + "/" + j.ToString() + ".png");
        imageBytes = await webClient.DownloadDataTaskAsync(uri);

【问题讨论】:

    标签: c# xamarin.ios webclient system.net.webexception


    【解决方案1】:

    在 iOS 上我们可以使用NSUrlSession 来下载图片,这可能对你更有帮助。使用方法可以参考以下代码:

    NSUrlSession session = NSUrlSession.SharedSession;
    var dataTask = session.CreateDataTask(new NSUrlRequest(new NSUrl("yourUrl")), (data, response, error) =>
    {
        if (response != null)
        {
            DispatchQueue.MainQueue.DispatchAsync(() =>
            {
                MyImageView.Image = UIImage.LoadFromData(data);
            });
        }
    });
    
    dataTask.Resume();
    

    【讨论】:

    • 你能分享一个更完整的例子吗?在如何使用它
    猜你喜欢
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多