6

Sometimes images picked from the photo album with UIImagePickerController are cropped differently than how the user wants to crops it. This happens in approx. 1 of 50 image uploads.

When it happens the images are always cropped to a part of the image from the top left corner. Here is an example image with (1) showing in the red rectangle what the user supposedly selects to crop and (2) what image ends up on the server.

enter image description here

The selection in (1) is assumptive because it's unknown how the users exactly positions the crop and it was not possible to reproduce this incorrect cropping yet. It has only been observed with the live app. Some users tried to upload the same image multiple times always with the same incorrect crop and eventually complained, so it's not that users deliberately crop images like this.

Some users tried to upload different images and all of them were incorrectly cropped.

Here is the code (simplified but nothing more happens to the image):

class ImagePicker {

    private let imagePicker = UIImagePickerController()

    func showPicker() {
        imagePicker.sourceType = .PhotoLibrary
        imagePicker.mediaTypes = [kUTTypeImage as String]
        imagePicker.allowsEditing = true
        imagePicker.delegate = delegate
        imagePicker.modalPresentationStyle = .OverFullScreen
        parentViewController.presentViewController(imagePicker, animated: true, completion: nil)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
            uploadImage(image)
        }
    
        picker.dismissViewControllerAnimated(true, completion: nil)
    }

    func uploadImage(image: UIImage) {

        let imageData = UIImageJPEGRepresentation(image, 0.75)!
        let imageFile = PFFile(name: "image.png", data: imageData)
        
        // Upload to Open Source Parse Server which stores the image in an Amazon S3 bucket.
        let imageObject = PFObject(className: "ImageClass")
        imageObject(imageFile, forKey: "imageFile")
        imageObject.saveInBackground()
    }
}

Does anyone know why this happens?

Update:

I was able to reproduce the issue on an iPad, I will update here what the outcome was.

Update:

The issue occurred only on iPads so it is presumably related to a bug in the UIImagePickerViewController when cropping an image.

Community
  • 1
  • 1
Manuel
  • 14,274
  • 6
  • 57
  • 130
  • Facing the same issue. Did you solve it? It would be appreciated if you can help to solve this issue. – Ajay Gabani Dec 12 '17 at 06:02
  • @AjayGabani It doesn't happen anymore, but we are now showing a custom image cropper to the user after they picked an image from the `UIImagePickerController`. So there is one more processing step for the image where we draw and crop it in a `CGGraphicsContext`. As far as I remember that worked around the issue. If you can reproduce the issue, try a step-by-step debugging: inspect the picked image by putting a breakpoint in the delegate method, inspect the image data, etc. Somewhere along the flow it gets messed up. – Manuel Dec 12 '17 at 21:15
  • I searched this online but found nothing related to this. Now i have to go with custom crop classes too. – Ajay Gabani Dec 14 '17 at 10:28
  • @AjayGabani , did you solve this issue? I had faced with it too. –  Dmitriy Greh Feb 12 '18 at 09:07
  • @DmitriyGreh I used custom cropper to crop image. It will be easier and less time consuming. – Ajay Gabani Feb 12 '18 at 10:22
  • @Ajay Gabani, if you interested i solved it! You can see in comment below! –  Dmitriy Greh Feb 15 '18 at 12:35
  • @DmitriyGreh Thanks for the solution. I will use it next time. – Ajay Gabani Feb 16 '18 at 11:04

1 Answers1

0

I had the same issue. In my case, i solved it by change content mode of my imageView. It was set to "scaleAspectFill". i just removed it and my image become cropped right way.

Dmitriy Greh
  • 684
  • 8
  • 17
  • You mean a `UIImageView` in which you display the picked image? That wasn't the issue in my case where the resulting image was cropped wrongly. And only on iPads it seems, so I assume it is an iOS bug. Maybe the issue is already fixed with iOS 11? – Manuel Feb 15 '18 at 12:41