Error using UIImagePickerController under NavigationController and tabBarController - ios

There are 3 UIViewControllers under NavigationController and tabBarController.
Push the viewControllers like this: A->B->C
In viewController C, add the following codes to let user to select image from photo library.
func btnClicked() {
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
self.present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage{
topIamge.image = image
}
picker.dismiss(animated: false, completion: nil)
}
Problem is that after the user select an image or cancel the selection, then press the left bar button going back to B, an error Unbalanced calls to begin/end appearance transitions for shows up, if continue going back to A, the error is printed out again.
Did I make anything wrong?
P.S. There is no error if I press the back button without presenting UIImagePickerController.
Thx.

I'm sure it doesn't related UIImagePickerController
Check these solution :
https://stackoverflow.com/a/12230777/6131436
https://stackoverflow.com/a/20925686/6131436

Related

How to use "Use video" / "choose" button from video in swift 3?

I'm trying to upload my video chosen from library or recorded from camera to firebase. When I get the video from library there's a "choose" button on the image picker or when I record the video it says "Use video".
I think to run those buttons function you have to write the didFinishPickingMediaWithInfo function. But nothing happens when I record a video and click "choose" this is what I have as my code:
if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
//if the camera is available, and if the rear camera is available, the let the image picker do this
imagePicker.sourceType = .camera
imagePicker.mediaTypes = [kUTTypeMovie as String]
imagePicker.allowsEditing = false
imagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
imagePicker.videoMaximumDuration = 60
imagePicker.videoQuality = .typeIFrame1280x720
present(imagePicker, animated: true, completion: nil)
} else {
postAlert("Rear camera doesn't exist", message: "Application cannot access the camera.")
}
} else {
postAlert("Camera inaccessable", message: "Application cannot access the camera.")
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("didFinishPickingMediaWithInfo")
}
I want it to at least print to know that the video "choose button" is working. Any ideas on how to get the choose button to work? Any help would be kind. Thank you in advance!

Generic - Creating an image format with an unknown type is an error - Swift 3

I have an image picker controller. I want to select an image from the photo library and place it in the rounded image view. The image gets placed successfully in the image view but still I get this error "[Generic] Creating an image format with an unknown type is an error".
I have seen many other questions for this but none of them solves my problem. All the questions say that didFinishPickingMediaWithInfo wasn't called but mine is not the case. My image is shown in the image view and I still get the error.
I have set the UIImagePickerControllerDelegate and UINavigationControllerDelegate. Picker is defined as follows:
let picker = UIImagePickerController()
Delegate is set in viewDidLoad as follows:
picker.delegate = self
Photo Library is opened as:
self.picker.allowsEditing = false
self.picker.sourceType = .photoLibrary
self.picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(self.picker, animated: true, completion: nil)
My code of didFinishPickingMediaWithInfo is :
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
userImageView.image = image
} else{
print("Something went wrong")
}
self.dismiss(animated: true, completion: nil)
}
I am using xcode 8 and swift 3. Please help.

How to set an image from UIImagePickerController to UIimages in multiple view controllers

New to coding, I've figured out how to let a user select a photo as their background with the following code
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
backgroundImage.contentMode = .ScaleAspectFill
backgroundImage.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
// Wallpaper function
#IBAction func wallpaperMenuPressed(sender: AnyObject) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
And it works, still haven't figured out how to save it but will find out soon.
But, if I have a backgroundImage in all my views, how do I get it to set the same image for all of them?
Thanks
I would suggest making use of NSNotificationCenter.
In each of your view controllers where you need to apply the change you listen for a notification:
let nc = NSNotificationCenter.defaultCenter()
nc.addObserver(self, selector: "UpdateBackgroundImage:", name: "BackgroundImageChanged", object: image)
In each of those view controllers you'll need to implement the selector, UpdateBackgroundImage in this case. They would be similar to:
func UpdateBackgroundImage(notification: NSNotification) {
let backgroundImage = notification.userInfo!["image"] as UIImage
// Your code to assign image to background
}
After you save the image you can post a notification:
// Right after you assign backgroundImage.image = pickedImage
let nc = NSNotificationCenter.defaultCenter()
let myDict = [pickedImage, "image"]
nc.postNotificationName("BackgroundImageChanged", object: nil, userInfo: myDict)
The nice thing about using NSNotificationCenter is that as you add new views that need to update the background images you just add the first bit of code. You don't need to update the code where you pick the images.
I suggest you to create a BaseViewController in which common functionality of your app is there and also take a imageView in that.
After that inherit all your viewControllers form BaseViewController so that imageView also be inherited and you can set it and it will be same all of your viewControllers inherited from BaseViewController.

Selecting image from library doesn't call "didFinishPickingImage" function - Swift

I have a UIImagePickerController with the option to select from library.
When the accessLibrary button is called, it prompts another UIImagePickerController that has source type .PhotoLibrary.
func accessLibrary(sender: UIButton!) {
NSLog("access library")
choseFromLibrary = true
self.libraryPicker = UIImagePickerController()
self.libraryPicker!.allowsEditing = false
self.libraryPicker!.sourceType = .PhotoLibrary
self.presentViewController(self.libraryPicker!, animated: true, completion: nil)
}
This all works well, but when the user does select a photo, I want to pass that image to a view and then show that view on the screen.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary
var image = info[UIImagePickerControllerOriginalImage] as? UIImage
if (image != nil) {
if (choseFromLibrary) {
let cnfrmImgView = ConfirmImageView.init(image: image!)
self.view.addSubview(cnfrmImgView)
}
else {
if (frontFacing) {
image! = UIImage(CGImage: image!.CGImage!, scale: 1.0, orientation: .LeftMirrored)
}
let cnfrmImgView = ConfirmImageView.init(image: image!)
self.view.addSubview(cnfrmImgView)
}
}
}
The above function works if the image is taken using the camera, but not using the library.
When a user picks a photo from the library, the library picker controller just dismisses itself and the view is not display.
I ran an NSLog("here") to check if the didFinishPickingImageWithInfo function is called when choosing from the library and in fact it is not.
didFinishPickingImageWithInfo is a delegate message (UIImagePickerControllerDelegate). The problem, however, is that you have not given your image picker any delegate! Thus, it has no one to send delegate messages to.
if you want to implement a Instagram like image picker than try this
Image Picker like Instagram
The delegate function is updated in new swift versions from 4 to upwards.
Use this function instead
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { }

UIImagePicker - Do not allow panorama to be picked

Looking for help with an image picker. The picker is functioning properly - however - it allows the user to pick from Panorama photos as well as normal photos on their device.
I want to have the picker only allow the user to choose non panorama pictures.
I looked through the dev document and couldn't find anything about this. Any help appreciated! Here's my code:
#IBAction func selectImage(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType =
UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.mediaTypes = [kUTTypeImage as NSString]
imagePicker.allowsEditing = false
self.clearBtn.hidden = false
self.presentViewController(imagePicker, animated: true,
completion: nil)
}
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.postImage.contentMode = .ScaleAspectFit
self.postImage.image = image
self.hasPicture = true
}
I know that changing the UIImagePickerControllerSourceType to .PhotoLibrary will only allow picking from the camera roll, but I was hoping to still allow shared photo streams and such that just weren't panoramic.
As Christian posted in a comment - there is no native way for a UIImagePicker to only allow certain types of pictures (excluding panorama).
You have to handle validating the image after it is returned.

Resources