首页 > 解决方案 > Xamarin:CGImageSource.FromUrl 返回 null

问题描述

我在我的应用程序中使用以下代码。

string imageUrl = "https://imageUrl.png/"; //This is a valid url. I can see the image when I paste the url into my web browser
var url = new NSUrl(imageUrl);
using (CGImageSource source = CGImageSource.FromUrl(url))
{
    if (source != null)
    {
        //I never get here
    }
}

为什么source总是为空?我尝试了很多不同imageUrls的方法,结果都一样。

标签: xamarinxamarin.ioscgimageios11.4

解决方案


你的照片地址好像是HTTPS前缀,和HTTP不一样。你可以试试别的URL是HTTP前缀。如果地址必须是HTTPS,我建议你使用一些SDK。

或者您可以使用以下代码,它们在使用 http 时有效

string str = ""; //your image url

NSData ImageData = NSData.FromUrl(new NSUrl(str) );
UIImage image = new UIImage(ImageData);
   if (image != null)
        {
            Console.Write("success");
        }
        else
        {
            Console.Write("fail");
        }

推荐阅读