简体   繁体   中英

Xamarin iOS where are images from gallery

I'm looking for a way to loop for each image that issaved on the iOS device.

I have already tried with

var library = new ALAssetsLibrary();

library.Enumerate(ALAssetsGroupType.Library, GroupEnumerator, Console.WriteLine);
library.Enumerate(ALAssetsGroupType.Album, GroupEnumerator, Console.WriteLine);
library.Enumerate(ALAssetsGroupType.SavedPhotos, GroupEnumerator, Console.WriteLine);
library.Enumerate(ALAssetsGroupType.All, GroupEnumerator, Console.WriteLine);

But when I debug all list are empty.

How can I retrieve a list of all images saved in the device (gallery)?

Here are complet class code from where i (try to) list all images

using AssetsLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace AG.iOS.Services
{
    public class iOSGalleryContent : IGalleryContent
    {
        public static readonly List<string> ImageExtensions = new List<string> { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" };
        List<string> ImagesNames = new List<string>();
        public List<string> GetImagesNames()
        {
            var library = new ALAssetsLibrary();
            library.Enumerate(ALAssetsGroupType.Library, GroupEnumerator, Console.WriteLine);
            library.Enumerate(ALAssetsGroupType.Album, GroupEnumerator, Console.WriteLine);
            library.Enumerate(ALAssetsGroupType.SavedPhotos, GroupEnumerator, Console.WriteLine);
            library.Enumerate(ALAssetsGroupType.All, GroupEnumerator, Console.WriteLine);

            return ImagesNames;



        }

        private void GroupEnumerator(ALAssetsGroup group, ref bool shouldStop)
        {
            if (group == null)
            {
                shouldStop = true;
                return;
            }
            if (!shouldStop)
            {
                group.Enumerate(AssetEnumerator);
                shouldStop = false;
            }
        }

        private void AssetEnumerator(ALAsset asset, nint index, ref bool shouldStop)
        {
            if (asset == null)
            {
                shouldStop = true;
                return;
            }
            if (!shouldStop)
            {
                ImagesNames.Add(asset.AssetUrl.AbsoluteString);
                Console.WriteLine(String.Format("Item[{0}] : {1}", index, asset.ToString()));
                shouldStop = false;
            }
        }
    }
}

Here's a minimal code example that gets images from all albums. It should be relatively easy to modify this for your purposes. I haven't tried the code but it should be ok.

assetsLibrary = new ALAssetsLibrary();
photoAssets = new List<ALAsset>();

assetsLibrary.Enumerate (ALAssetsGroupType.Album, (ALAssetsGroup group, ref bool stop) => {
    group.SetAssetsFilter (ALAssetsFilter.AllPhotos);
    group.Enumerate ((ALAsset asset, nint index, ref bool st) => {
        int notfound = Int32.MaxValue;
        if (asset != null && index != notfound) {
            photoAssets.Add (asset);
        }
    });
});

Xamarin has a Xamarin.iOS sample that does this: MediaNotes

Strangely the code work perfectly...Debuger point me to wrong direction because he can not happen to show me the content of the stringUrl List.

So, be free to use it. It does what it is supposed to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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