stormlion227 / ImageCropper.Forms

Xamarin.Forms plugin to crop and rotate photos.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ImageCropper.Forms: How to remove the multiple pop-up?

sreejithsree139 opened this issue · comments

I am using ImageCropper.Forms for cropping the image selected from the camera and gallery. I have 3 options for changing a profile picture; Take Photo, Upload from Gallery and Select Avatar. So for showing the Media options I am using a DisplayActionSheet like below:

var actionSheet = await DisplayActionSheet(null,"Cancel", null, "Take Photo", "Upload from Gallery", "Select Avatar");
if (actionSheet == "Take Photo")
{
	OpenCamera();
}
else if(actionSheet == "Upload from Gallery")
{
	Opengallery();
}
else if(actionSheet == "Select Avatar")
{
	OpenAvatar();
}

OpenCamera() and Opengallery() are added below. That code is also asking the Take Photo and Photo Library options again as a popup. I don't need that pop up, is there any way to remove the second popup asked by ImageCropper.Forms and load the camera or gallery directly?

My Code:

    async void OpenCamera()
	{
		try
		{
			await CrossMedia.Current.Initialize();
            //I need to open camera only here, no need of a pop up again.
			new ImageCropper()
			{
				PageTitle = "Test Title",
				AspectRatioX = 1,
				AspectRatioY = 1,
				CropShape = ImageCropper.CropShapeType.Rectangle,
				SelectSourceTitle = "Select source",
				TakePhotoTitle = "Take Photo",
				PhotoLibraryTitle = "Photo Library",
				Success = (imageFile) =>
				{
					Device.BeginInvokeOnMainThread(() =>
					{
						profilephoto.Source = ImageSource.FromFile(imageFile);
					});
				}
			}.Show(this);
		}
		catch (Exception ex)
		{
			System.Diagnostics.Debug.WriteLine("CameraException:>" + ex);
		}
	}

async void Opengallery()
{
	try
	{
		await CrossMedia.Current.Initialize();
        //I need to open gallery only here, no need of a pop up again.
		new ImageCropper()
		{
			PageTitle = "Test Title",
			AspectRatioX = 1,
			AspectRatioY = 1,
			CropShape = ImageCropper.CropShapeType.Rectangle,
			SelectSourceTitle = "Select source",
			TakePhotoTitle = "Take Photo",
			PhotoLibraryTitle = "Photo Library",
			Success = (imageFile) =>
			{
				Device.BeginInvokeOnMainThread(() =>
				{
					profilephoto.Source = ImageSource.FromFile(imageFile);
					//var stream = ImageSource.FromFile(imageFile);
					//imagefile = imageFile;
				});
			}
		}.Show(this);
	}
}

Screenshot: https://i.stack.imgur.com/qYBuu.jpg

Is there any way to separate camera and gallery code like MediaPlugin?

You can pass an image to the Show method and it will not prompt the user with the popup. Refer to the readme

Show​(​this​, ​imageFileName​);

And you can previously use Media.Plugin by your own to get the image directly from the source you want.