Skip to content

Instantly share code, notes, and snippets.

@danielkon96
Created September 20, 2019 22:56
Show Gist options
  • Save danielkon96/d3a76c9c98078c0de103a707bb78e804 to your computer and use it in GitHub Desktop.
Save danielkon96/d3a76c9c98078c0de103a707bb78e804 to your computer and use it in GitHub Desktop.
Select Images method
private async void SelectImagesButton_Clicked(object sender, EventArgs e)
{
//Check users permissions.
var storagePermissions = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
var photoPermissions = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Photos);
if (storagePermissions == PermissionStatus.Granted && photoPermissions == PermissionStatus.Granted)
{
//If we are on iOS, call GMMultiImagePicker.
if (Device.RuntimePlatform == Device.iOS)
{
//If the image is modified (drawings, etc) by the users, you will need to change the delivery mode to HighQualityFormat.
bool imageModifiedWithDrawings = false;
if (imageModifiedWithDrawings)
{
await GMMultiImagePicker.Current.PickMultiImage(true);
}
else
{
await GMMultiImagePicker.Current.PickMultiImage();
}
MessagingCenter.Unsubscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectediOS");
MessagingCenter.Subscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectediOS", (s, images) =>
{
//If we have selected images, put them into the carousel view.
if (images.Count > 0)
{
ImgCarouselView.ItemsSource = images;
InfoText.IsVisible = true; //InfoText is optional
}
});
}
//If we are on Android, call IMediaService.
else if (Device.RuntimePlatform == Device.Android)
{
DependencyService.Get<IMediaService>().OpenGallery();
MessagingCenter.Unsubscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectedAndroid");
MessagingCenter.Subscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectedAndroid", (s, images) =>
{
//If we have selected images, put them into the carousel view.
if (images.Count > 0)
{
ImgCarouselView.ItemsSource = images;
InfoText.IsVisible = true; //InfoText is optional
}
});
}
}
else
{
await DisplayAlert("Permission Denied!", "\nPlease go to your app settings and enable permissions.", "Ok");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment