Reputation: 935
I have already acquired the permission to access the Photo Gallery and I have found a wait to ask the user to select one or more images from the gallery and it works perfectly fine in iOS 13.
However, I can see that in some apps like Facebook, it shows all images in the gallery as a preview so that user can just tap on them without having to show him the whole library and prompt him to select them.
My objective is to have an app where it can find all images that have been created/modified in the past 24 hours so that the user can upload them to server with one tap without having to force the user to choose them from the library.
I am using Xamarin.Forms but this code will be only used in Xamarin.iOS part of the project using DependencyService so I am not interested in the Android part (at least for now).
Also, if anyone has achieved the same using Swift, I believe it would be possible to rewrite it in C#.
Thanks!
Upvotes: 0
Views: 627
Reputation: 935
I answer my own question in case anybody else is looking for it:
Use Photo Library to acquire all the pictures in your gallery. Firstly, you need to add the permission string in the info.plist:
<key>NSPhotoLibraryUsageDescription</key>
<string> photos description. </string>
Secondly, request the dynamic authorisation like:
PHPhotoLibrary.RequestAuthorization((status) =>
{
switch (status)
{
case PHAuthorizationStatus.Authorized:
{
Debug.WriteLine(“Authorized to access”);
break;
}
case PHAuthorizationStatus.Denied: case PHAuthorizationStatus.Restricted:
{
Debug.WriteLine(“Not allowed”);
break;
}
default:
{
Debug.WriteLine(“Not determined”);
break;
}
}
});
At last, you can request the photos like:
PHImageRequestOptions options = new PHImageRequestOptions();
options.ResizeMode = PHImageRequestOptionsResizeMode.Fast;
options.DeliveryMode = PHImageRequestOptionsDeliveryMode.FastFormat;
options.Synchronous = true;
var assets = PHAsset.FetchAssets(PHAssetMediaType.Image, null);
PHImageManager manager = new PHImageManager();
List<UIImage> images = new List<UIImage>();
foreach (PHAsset asset in assets)
{
manager.RequestImageForAsset(asset, PHImageManager.MaximumSize, PHImageContentMode.Default, options,
(image, info) =>
{
images.Add(image);
});
}
Upvotes: 1