Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePickerController displaying wrong crop when editing is allowed

I am having an issue with the UIImagePickerController, I have enabled editing, but what is happening is when the user crops the image, there is a y offset between what the square crop shows the user will be the crop, and what the actual crop is. It only works out at about 20 pixels, but is now a big issue. I have included two screenshots that demonstrate the issue. In the first, the crop square seems to extend above the image, but when the inage is chosen the top of the image is set properly (the image is just a screenshot so the top of the image is the top of the status bar). In the second screenshot, if you try and put the crop to then very bottom of the photo, it springs back to this position, so the user thinks the bottom of the image is not included, when in fact it is, when it is chosen. Bit of a headache. Seems to do the same in the simulator as on device. Anyone know why this might be?

Top cropbottom crop

like image 362
TimWhiting Avatar asked Nov 01 '22 10:11

TimWhiting


1 Answers

This doesn't solve the display issue where it shows the cropping box 20px off, but you can side-step the final-cropped image bug by doing the following when the user selects an image:

// MARK: - UIImagePickerControllerDelegate

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: { [weak self] in self?.didCompleteSelection(image: nil) })
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    picker.dismiss(animated: true, completion: { [weak self] in
        guard
            let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage,
            let cropRect = info[UIImagePickerControllerCropRect] as? CGRect
        else { return }

        //crop with status-bar offset due to iOS bug introduced in iOS 8 with status bar offset
        let offsetRect = cropRect.offsetBy(dx: 0, dy: UIApplication.shared.statusBarFrame.size.height)
        guard let croppedImage = originalImage.cgImage?.cropping(to: offsetRect) else { return }

        self?.didCompleteSelection?(image: croppedImage)
    })
}
like image 142
Albert Bori Avatar answered Nov 12 '22 18:11

Albert Bori