I have a simple standard image-picker that works fine except that when I use the editor function the image is shifted above the square crop frame. When I select the image or resize the image then accept it the cropped image has about 20 or so pixels of image above what the crop frame and about 40 pixels hacked off the bottom. Below is the code. Because a picture is worth a thousand words I have included two screen shots to show what it looks like in the edit mode and what I get back. I would appreciate any suggestions code wise or setup wise that allow the editor to crop the picture accurately.
import UIKit
class ImagePickerVC: UIViewController {
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var btnPickImage: UIButton!
@IBOutlet weak var btnCancel: UIBarButtonItem!
@IBOutlet weak var btnSave: UIBarButtonItem!
var imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
setupNavBarAppearance(control: self)
imagePicker.delegate = self
myImage.image = myUserProfile.avatar
}
@IBAction func btnCancel(_ sender: Any) {
dismiss(animated: false, completion: nil)
}
@IBAction func btnPickImage(_ sender: Any) {
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
present(imagePicker, animated: false, completion: nil)
}
@IBAction func btnSave(_ sender: Any) {
myUserProfile.avatar = myImage.image!
dismiss(animated: false, completion: nil)
}
}
extension ImagePickerVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
myImage.image = image
}
dismiss(animated: false, completion: nil)
}
}
The Status Bar is messing it up.
Adding the following extension worked to make the UIImagePickerControllerEditedImage
correctly crop the image.
extension UIImagePickerController {
open override var childViewControllerForStatusBarHidden: UIViewController? {
return nil
}
open override var prefersStatusBarHidden: Bool {
return true
}
}
This extension hides the status bar in UIImagePickerController
which looks a little weird but the result is all that matters.
I got it from Swift 3.0 - how to hide status bar after calling UIImagePickerController?
Thanks siefix! This works perfectly.
Swift 4.2
extension UIImagePickerController {
open override var childForStatusBarHidden: UIViewController? {
return nil
}
open override var prefersStatusBarHidden: Bool {
return true
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With