简体   繁体   English

如何从 Xamarin.iOS 中的 URL 加载图像

[英]How to Load an Image from URL in Xamarin.iOS

When I pick a picture from Gallery I have something like this:当我从图库中选择图片时,我有这样的事情:

NSUrl referenceURL = e.Info[new  
NSString("UIImagePickerControllerReferenceURL")] as NSUrl;
//imageURL = referenceURL.AbsoluteString;
imageURL = referenceURL.AbsoluteUrl.ToString();
string path = referenceURL.Path;

NSUrl url1 = referenceURL.FilePathUrl;
NSUrl url2 = referenceURL.FileReferenceUrl;

Now I'm trying to load that Image from the URL:现在我正在尝试从 URL 加载该图像:

var url = new NSUrl(imageURL);
var data = NSData.FromUrl(url);
defectImage.Image = UIImage.LoadFromData(data);

But data is still null.data仍然为空。

What is the correct way to load an Image from URL?从 URL 加载图像的正确方法是什么?

Note: url1 and url2 are just examples.注意:url1 和 url2 只是示例。 path is something like = "/imagename.jpg" path类似于 = "/imagename.jpg"

Maybe I'm retrieving the URL on a wrong way?也许我以错误的方式检索 URL? there are so many methods to get a path/url.有很多方法可以获取路径/网址。

Since these image urls are "assets-library" based, so you need to go through the ALAssetsLibrary to access them:由于这些图像 url 是基于“assets-library”的,因此您需要通过ALAssetsLibrary来访问它们:

var url = e.Info[UIImagePickerController.ReferenceUrl] as NSUrl;
var asset = new ALAssetsLibrary();
UIImage image;
asset.AssetForUrl(
    url, 
    (ALAsset obj) =>
    {
        var assetRep = obj.DefaultRepresentation;
        var cGImage = assetRep.GetFullScreenImage();
        image = new UIImage(cGImage);
        // Do something with "image" here... i.e.: assign it to a UIImageView
        imageView.Image = image;
    }, 
    (NSError err) => 
    { 
        Console.WriteLine(err);
    }
);

Note: You really should start using the Photos framework if you do not have to support iOS 7 clients....注意:如果你不需要支持 iOS 7 客户端,你真的应该开始使用照片框架......

The Assets Library framework is deprecated as of iOS 9.0.自 iOS 9.0 起,资产库框架已弃用。 Instead, use the Photos framework instead, which in iOS 8.0 and later provides more features and better performance for working with a user's photo library.相反,请改用照片框架,它在 iOS 8.0 及更高版本中提供更多功能和更好的性能来处理用户的照片库。

Ref: https://developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/index.html参考: https : //developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/index.html

您可以使用 nuget 包 FFImageLoading :

ImageService.Instance.LoadUrl("url").Into(YourUIImageView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM