9

I'm using a UIImagePicker to get the user to take a photo. When the photo is taken, I want them to pan and zoom the image around to fit inside the cropped box so that the image is stored as a square.

However, when cropping the image, it seems as though you cannot move it to the top and bottom of a (portrait) image (left and right if landscape).

I have tried searching but there doesn't seem to be much information, but it seems like a massive issue.

Can someone help?

This is the very small amount of code I'm using:

let imagePicker = UIImagePickerController()

imagePicker.allowsEditing = true
imagePicker.sourceType = UIImagePickerControllerSourceType.camera

present(imagePicker, animated: true, completion: nil)

There's obviously more code but this is the main part.

EDIT with photo:

enter image description here

So I want to be able to move the photo around/zoom in to select any square portion to save. However, I cannot move it from this position/ keeps snapping back.

I can zoom in, but it still restricts me from the top and bottom edges.

Again, it works with the photoLibrary.

user2397282
  • 3,798
  • 15
  • 48
  • 94
  • let myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.allowsEditing = true myPickerController.sourceType = UIImagePickerControllerSourceType.Camera self.presentViewController(myPickerController, animated: true, completion: nil) – Himanshu Moradiya Jan 24 '17 at 09:14
  • Are you just telling me to set the delegate to self? Because I've already done that – user2397282 Jan 24 '17 at 09:51
  • are you checking in which device ? iphone 6 or else ? – Himanshu Moradiya Jan 24 '17 at 09:55
  • It only needs to work for iPhone 5S – user2397282 Jan 24 '17 at 10:02
  • then in iphone 5s i never face that kind of problem .one suggestion is that remove your app from your device and then clean your project and run it again and check this issue again. – Himanshu Moradiya Jan 24 '17 at 10:08
  • Still having the same issue. – user2397282 Jan 24 '17 at 10:47
  • 1
    This has been a bug since iOS 6, and it does not look like they're gonna fix it for some reason. I recommend using an alternative, as https://github.com/justwudi/WDImagePicker for example. Also look at this post back from 2012, with the same exact bug: http://stackoverflow.com/questions/12630155/uiimagepicker-allowsediting-stuck-in-center – Imbue Jan 24 '17 at 14:08
  • @HimanshuMoradiya lol. Have you tried it? It is a bug hat has existed since ios6. Just try it before claiming it works for you. – Fogmeister Jan 26 '17 at 09:34
  • Does this answer your question? [UIImagePicker allowsEditing stuck in center](https://stackoverflow.com/questions/12630155/uiimagepicker-allowsediting-stuck-in-center) – alpere Sep 29 '22 at 11:49

4 Answers4

9

This is a bug that was introduced in iOS 6 and hasn't been fixed yet.

A radar was raised in 2012 for this but closed by Apple. I managed to get it opened again and have been pestering Apple devs in my contacts for the past 6 months.

http://openradar.appspot.com/12318774

Until this is fixed by Apple the only option is to use a third party control or do it yourself.

Here is the radar I opened...

http://openradar.appspot.com/28260087

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
0

I know you said it already zooms but you may just need to adjust the bounds for that.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    image.image = info[UIImagePickerControllerEditedImage] as? UIImage
    self.dismiss(animated: true, completion: nil)
}

You may also need to use CGRect to set the image correctly in the screen. It should be centered. If you are using and iphone 5 the dimensions 640 x 1136.

This happens because the width or the height of the image gets maxed out to the screen.

Jarad Kears
  • 114
  • 2
  • 11
0

I have used the solution provided at below link:-

https://github.com/Hipo/HIPImageCropper

It handles the landscape and potrait allignment of the image and provides zoom in and zoom out with crop functionality.

Hope this helps.

iAviator
  • 1,310
  • 13
  • 31
0

If you have set "View controller-based status bar appearance" to NO in info.plist and set status bar appearance as light using

UIApplication.shared.statusBarStyle = .lightContent

or using any other method , Then simply set the style as .default before presenting the image picker. for Eg:

imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
UIApplication.shared.statusBarStyle = .default
present(imagePicker, animated: true, completion: nil)

Change the source type according to your need either as photoLibrary or camera and in completion block of your didFinishPickingMediaWithInfo add the following to completion block.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    //let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

    var pickedImage : UIImage?
    if let img = info[UIImagePickerControllerEditedImage] as? UIImage
    {
        pickedImage = img

    }
    else if let img = info[UIImagePickerControllerOriginalImage] as? UIImage
    {
        pickedImage = img
    }
    dismiss(animated: true, completion: {
        UIApplication.shared.statusBarStyle         = .lightContent
    })}

Apparently this is a workaround for the same.Hope this helps.

SwiftNinja95
  • 157
  • 2
  • 16