[英]Xamarin iOS where are images from gallery
I'm looking for a way to loop for each image that issaved on the iOS device.我正在寻找一种方法来循环 iOS 设备上的每个图像。
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 Xamarin 有一个 Xamarin.iOS 示例可以执行此操作: 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.奇怪的是,代码运行良好......调试器指向错误的方向,因为他无法向我展示 stringUrl 列表的内容。
So, be free to use it.所以,请随意使用它。 It does what it is supposed to do.
它做它应该做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.