1

I want to get UIImage from Asset Library Url

I'm an developing app in xamarin ios, trying to get a UIImage Object from a given Asset-Library-Url.

Picked multiple images from gallery using ELCImagePickerViewController. All image paths are added in string array list.
Now i need to get all the images in Byte[] using the Asset-Library-Url path.

The given Url is:

assets-library://asset/asset.JPG?id=0E126068-14F9-431F-91B5-8BC397727656&ext=JPG

I tried it with the following approach :

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

But it always returns null. Can anybody please tell me what i'm doing wrong here?

MinistryOfChaps
  • 1,458
  • 18
  • 31
Santu
  • 13
  • 4
  • refer to https://stackoverflow.com/questions/3837115/display-image-from-url-retrieved-from-alasset-in-iphone – ColeX Oct 30 '17 at 07:27

1 Answers1

1

These Urls are for the ALAssetsLibrary, you will need to use the ALAssetsLibrary to access them. See example below:

public Byte[] LoadAssetAsByteArray(string uri)
{
    var nsUrl = new NSUrl(uri);
    var asset = new ALAssetsLibrary();
    Byte[] imageByteArray = new Byte[0];
    UIImage image;

    asset.AssetForUrl(nsUrl,(ALAsset obj) => {

      var assetRep = obj.DefaultRepresentation;
      var cGImage = assetRep.GetFullScreenImage();
      image = new UIImage(cGImage);
      // get as Byte[]
      imageByteArray = new Byte[image.AsPNG().Length];
      //imageView.Image = image;
    }, 
    (NSError err) => { 
        Console.WriteLine(err);
    });

    return imageByteArray;
}

There is a great recipe using a UIImagePickerController on the Xamarin Site which I beleive would help you

groveale
  • 467
  • 3
  • 11