UIImagePickerController AllowsEditing not working
Asked Answered
N

6

20

I am using UIImagePickerController to allow the user to take a picture. I want to allow him/her to edit it afterwards but, whatever I do, I get nothing.

Here is my code (I am using Xamarin):

UIImagePickerController imagePicker = new UIImagePickerController ();

// Set our source to the camera
imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;

// Set what media types
//imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes (UIImagePickerControllerSourceType.Camera);
// show the camera controls
imagePicker.ModalPresentationStyle = UIModalPresentationStyle.CurrentContext;
imagePicker.ShowsCameraControls = true;
imagePicker.AllowsEditing = true;
imagePicker.SetEditing (true,true);
imagePicker.PreferredContentSize = new SizeF(900,600);
imagePicker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
imagePicker.Title="taste.eat. image";

// Attach the delegate
imagePicker.Delegate = new ImagePickerDelegate();

// Show the picker
NavigationController.PresentViewController(imagePicker, true,null);

Am I missing something?

I have followed the tutorial and I am getting to the screen with the rectangle, but if I pan or zoom it just snaps back to the center once I lift my finger. Is it possible to get to this screen from the photos application?

Image from the photos application

Near answered 9/1, 2014 at 15:42 Comment(3)
Hey, have you figured it out? ive got the same problem.Kiwanis
From my understanding allowsEditing simply allows you to crop the photo. It does not actually bring up the iOS photo editing functionality in the Photo App, as you show in the screenshotRuperto
It only allows you to crop. Sorry.Cooperman
C
48

When using UIImagePickerController's delegate method - imagePickerController:didFinishPickingMediaWithInfo:, we get the image using

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

This code will always return the original image, even if editing is ON.

Try using

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

This will return the edited image if editing is ON.

Conto answered 20/5, 2014 at 12:14 Comment(1)
This simple mistake on my end was driving me mad, thanks for the help!Gibby
R
29

The AllowsEditing property simply allows the user to crop to a square if picking an image and trim the video if picking a video.

Any other functionality needs to be implemented with custom UI and code.

See this question:iPhone SDK - How to customize the crop rect in UIImagePickerController with allowsEditing on?

What you are showing in the screenshot is not part of UIImagePickerController, unfortunately

Ruperto answered 15/5, 2014 at 21:0 Comment(0)
G
7

Swift 3

I was having a hard time returning the cropped image (simple mistake on my end). Instead of using UIImagePickerControllerOriginalImage, you need UIImagePickerControllerEditedImage. See below:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    // The info dictionary contains multiple representations of the image, and this uses the cropped image.
    let selectedImage = info[UIImagePickerControllerEditedImage] as! UIImage

    // Set yourImageView to display the selected image.
    yourImage.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}
Gibby answered 7/11, 2016 at 18:34 Comment(0)
M
4

Swift 4+

There have been some changes after Swift 4. UIImagePickerControllerEditedImage changed to UIImagePickerController.InfoKey.editedImage.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
    imageView.image = selectedImage
    dismiss(animated: true, completion: nil)
}
Munda answered 8/3, 2019 at 17:34 Comment(0)
C
0

There isn't any way to enable filters by just changing a property like allowsEditing = YES. It will only display a cropping tool. As per your screenshot, it looks like you have integrated some buggy open source library and without looking at the source code, it would be difficult to fix your center cropping bug.

It is better to post some concrete detail about your implementation or switch to standard open source library.

Cristencristi answered 22/5, 2014 at 11:31 Comment(0)
S
0

It will be work like this way.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let selectedImage = info[UIImagePickerControllerEditedImage] as! UIImage

    userPhoto.image = selectedImage

    dismiss(animated: true, completion: nil)
}
Samualsamuel answered 15/10, 2017 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.