UIImagePickerController edited image retrieves wrong image in iPad - ios

I have UIImagePickerController to select/pick an image from the photo gallery. I have allowed UIImagePickerController to edit (selected) image (crop square frame of image), which works fine with iPhone but iPad can't provide properly edited (cropped) image.
I tried the following suggestion but could not find a solution:
iPad iOS7 - UIImagePickerController in UIPopoverController has wrong preview image
Here is code, that I've tried:
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
// Update (zoom) visibility of image using pinch gesture & retrieve image using delegate - didFinishPickingMediaWithInfo
//------------------------------------------
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
// Wrong image - retrieves image different than actual visible in the editing window of picker view.
}
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
// Original image, visible perfect in iPad, when imagePicker.allowsEditing = false
}
picker.dismiss(animated: true) {
// dismissing image picker
}
}

Add this method , you will get accurate result.
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
UIApplication.shared.isStatusBarHidden = true
}

Related

UIImagePickerController returns image with black background on right side when the property .allowsEditing is set to true

I'm using UIImagePickerController for picking images from gallery. Here is the code
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
isfrompicker = true
if let image = info[.editedImage] as? UIImage {
// I'm getting image with black background on right side as shown.
}
}
Link to image
I'm not sure why I'm getting black background on right side. Same code is working fine on some devices. But I'm getting issue with my iPhone X.
I got the same problem a few ago.
Then i found a solution on
Link to page
Try adding this line before presenting imagePicker
UINavigationBar.appearance().isTranslucent = true

One UIImagePickerController for multiple ImageVIews

could you please help me with UIImagePickerController
I have 12 ImageViews in 1 ViewController. I need help so that each ImageView could pick different photos from Library. Someone told me to use Tags for them but I can't make it work cause i'm new in Swift.
Enable User Interaction Enabled for each UIImageView on a Storyboard.
Add TapGestureRecogniser to each UIImageView. Connect each TapGestureRecogniser with IBAction.
#IBAction func tap(_ sender: UITapGestureRecognizer) {
currentImageView = sender.view as! UIImageView
let picker = UIImagePickerController()
picker.delegate = self
self.present(picker, animated: true, completion: nil)
}
Define variable to store current UIImageView
private var currentImageView: UIImageView? = nil
Handle image selection and assign the image to currentImageView
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage]
currentImageView?.image = image as! UIImage
self.dismiss(animated: true, completion: nil)
}
It's not about the swift. You set up a "tag" number in the Interface builder (or in the code. There is a .tag property on the UIResponder descendants).
After that you make a switch in your callback method like
To make it a bit clearer:
let picker: UIImagePicker! = {
// setup your image picker here, don't forget to set the proper delegate
}()
var lastSender: AnyObject?
func onTouch(_ sender: AnyObject?) {
// add checks for the sender here
lastSender = sender
present(picker, animated: true, completion: *Your Completion*)
}
Delegate:
in
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
let image = *Get Image From Info*
lastSender?.image = image

Is it possible to set dimensions for UIImagePickerController's editor?

I'm trying to make users choose a photo from their library and crop this photo to specific dimensions like a circular , 80 x 60 sized photo.
#IBOutlet var imageView: UIImageView!
#IBAction func choosePhoto(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePickerController.allowsEditing = true
self.present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let choosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = choosenImage
dismiss(animated: true, completion: nil)
}
I pick photos using this code but edit screen is a rectangle so it is not what i'm looking for.Is it possible to change the edit screen's dimensions ?
iOS doesn't have any circular crop feature by default. You will need to integrate external library for implementing crop feature.
You can make use of these for circular cropping https://github.com/kekearif/KACircleCropViewController https://github.com/TimOliver/TOCropViewController

Presenting UIImagePickerController from modal ViewController

Have such part of my storyboard:
So I want present from UserProfileController: UIViewController "Мой профиль", which is modal, (on the top on image) UIImageViewController.
My code:
extension UserProfileController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBAction func imagePressed(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
self.present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
picker.dismiss(animated: true, completion: nil)
userProfileImageView.image = image
}
}
Well, imagePickerController presented, but after I choose image I have dismissed both controllers (imagePickerController and parent - UserProfileControler).
What am I doing wrong? Thanks
P.S. May be problem is I'm showing "SideMenuNavigationController" as modal? (so advised in SideMenu framework documentation)
Try to create you UIImagePickerController reference in your class (as a variable)
// to choose an image from the Camera Roll
fileprivate lazy var imagePicker: UIImagePickerController = {
let picker: UIImagePickerController = UIImagePickerController()
picker.allowsEditing = false
picker.sourceType = .savedPhotosAlbum
picker.delegate = self
return picker
}()

Image shows up in iPhone simulator but not ipad - Swift

When I go to select an image from my camera roll and display it in my view it doesn't appear on the Ipad simulator. It works great on the iphone. Here is my code
//--CONTROLS HOW TO PICK THE IMAGE WHEN THIS BUTTON IS CLICKED--\\
#IBAction func chooseImage(sender: AnyObject) {
var image = UIImagePickerController()
image.delegate = self
image.navigationBar.tintColor = UIColor.whiteColor()
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
//--AFTER THE IMAGED IS PICKED THIS BRINGS BACK THE VIEW--\\
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
println("Image Selected")
self.dismissViewControllerAnimated(true, completion: nil)
imageToPost.image = image
photoSelected = true
}
Apparently, the UIImageView called imageToPost is not displayed correctly by the iPad simulator. Check your storyboard file and make sure the image view is correctly wired to the outlet.
Also, check any constraints that might push the image view off the screen.

Resources