Reputation: 80
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;
}
}
}
}
Upvotes: 1
Views: 1401
Reputation: 80
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.
Upvotes: 0
Reputation: 7189
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
Upvotes: 1