I am working on a chat app with picture messaging (supports Portrait and Landscape orientations), recently updated to Swift 2.3.
I am running into this problem where dismissing the UIImagePickerController results in the app crashing with a EXC_BREAKPOINT (in both cases "Use Photo" and "Cancel"), and cannot for the life of me figure out what is causing it. Removing the self.dismissViewControllerAnimated(true, completion: nil) line removes the crash (though leaves the picker on the screen), so I believe that the root of the problems lies there.
Here is how I set up and call the UIImagePicker:
let takePhotoAction = UIAlertAction(title: "Take a Photo", style: .Default, handler: { (alert: UIAlertAction!) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
let imagePicker = UIImagePickerController();
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
self.presentViewController(imagePicker, animated: true, completion: nil)
}
})
And here are the methods that handle the event that an image was picked.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
if image != nil {
print("Got image");
uploadToS3(image!);
}
self.dismissViewControllerAnimated(true, completion: nil)
}
Any help would be appreciated!
Related
I am using UIImagePicker for picking images, it forks fine for iPhone gallery to pick images. but when i test app in iPad in xcode here when i tap on image button then app is crashing, i dont know why.
here is the code:
#IBAction func addProfileBtnAction(_ sender: Any) {
let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
//Just dismiss the action sheet
}
actionSheetController.addAction(cancelAction)
//Create and add first option action
let takePictureAction: UIAlertAction = UIAlertAction(title: "TakePhoto", style: .default) { action -> Void in
//Code for launching the camera goes here
self.openCameraPicker()
}
actionSheetController.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "ChooseFromLibrary", style: .default) { action -> Void in
//Code for picking from Gallery goes herece
self.openPhotoGallery()
}
actionSheetController.addAction(choosePictureAction)
//Present the AlertController
self.present(actionSheetController, animated: true, completion: nil)
}
func openCameraPicker() {
picker.sourceType = UIImagePickerController.SourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
}
func openPhotoGallery() {
picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(picker, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
//pickedImage = image
imgPick.image = image
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
i have checked the crash point by giving breakpoint, when it comes to openPhotoGallery() then its crashing.
UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
please help me with the code. to pic images from iPad gallery as well.
For iPad add your code :
(under the let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) line)
actionSheetController.popoverPresentationController?.sourceView = self.yourView
Hope it helps...
I am using a UIImagePickerController in my program and it is effectively changing the image of an imageview i have added. However, whenever I restart this app and come back to the home screen, it is automatically resetting to the default image I had it to before, rather than the user selected image. How can I make it so that it records which image was last used, and reloads it every time the program starts?
var imagePicker = UIImagePickerController()
func chooseImage(_ sender: Any) { //function called with button press
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
}else{
print("Camera not available")
}
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Default", style: .default, handler: { (action:UIAlertAction) in
self.avatarImageView.image = UIImage(named: "Avatar.png")
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerEditedImage] as! UIImage
avatarImageView.image = image
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
Since the app is going out of memory, you'll need some kind of persistence mechanism for saving the image. The simplest way to do this would be to store the image in UserDefaults. This can be accomplished like this:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerEditedImage] as! UIImage
avatarImageView.image = image
UserDefaults.standard.set(UIImagePNGRepresentation(image), forKey: "avatarImage")
picker.dismiss(animated: true, completion: nil)
}
Then when you reopen the app you'll need to check whether you've previously saved an avatarImage in UserDefaults and load it from there:
// Could be in viewDidLoad or wherever else you load your image
override func viewDidLoad() {
if let imageData = UserDefaults.standard.object(forKey: "avatarImage") as? Data {
avatarImageView.image = UIImage(data: imageData)
}
}
I'm presenting an UIImagePickerController to take a photo, the problem is if I take a photo, or cancel the UIImagePickerController I'll return on a white screen and not on my previous ViewController. Only my navigation bar still here. If I click to an other view (with the navigation bar) it's ok, and if I return on the tab where the UIImagePickerController is called it still white.
let picker = UIImagePickerController();
picker.delegate = self;
picker.mediaTypes = [swiftString];
picker.allowsEditing = true
picker.sourceType = UIImagePickerControllerSourceType.camera;
picker.cameraCaptureMode = .photo
self.present(picker, animated:true, completion:nil);
dismiss :
picker.dismiss(animated: true, completion:nil);
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion:nil);
}
If someone have an idea thank's !
Update :
White screen appears always when i call a view with present method. So i think it's conflict with the navigation controller (hierarchy...)
This solve my problem :
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
appDelegate.navigationController.present(picker, animated: true, completion: nil)
If someone have an other alternative to solve it !
Use picker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext before presenting the imagepicker.
Here's the code I use for the camera:
func openCameraApp() {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,
animated: true,
completion: nil)
} else {
noCamera()
}
}
func noCamera(){
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .alert)
let okAction = UIAlertAction(
title: "OK",
style:.default,
handler: nil)
alertVC.addAction(okAction)
present(
alertVC,
animated: true,
completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage // do you have this line in your code?
image = chosenImage
self.performSegue(withIdentifier: "ShowEditView", sender: self)
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: false, completion: nil)
}
I notice a couple of things different.
You set .allowsEditing to true. I don't believe that is making a difference.
I'm checking for a camera, but you probably didn't put that code in your question.
I'm setting .modalPresentationStyle to full screen. This could be your issue.
I don't see the imagePickerController(picker: didFinishPickingMediaWithInfo) call. But since it shouldn't build, that definitely isn't the issue.
Inside that call I unwrap the image from info. I believe this is your issue. I've placed a comment on that specific line, asking you to check for this.
Finally, I'm executing a segue to another view controller. (My app is basically a "select" then "edit".)
Set the .modalPresentationStyle to fullScreen
I am creating an app that utilises this image picker code retrieved from: https://github.com/projectwakii/UIImagePickerController-in-Swift
Shown below:
#IBAction func imageButtonDidPress(sender: AnyObject) {
print("pressed")
//show the action sheet (i.e. the little pop-up box from the bottom that allows you to choose whether you want to pick a photo from the photo library or from your camera
let optionMenu = UIAlertController(title: nil, message: "Where would you like the image from?", preferredStyle: UIAlertControllerStyle.ActionSheet)
let photoLibraryOption = UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.Default, handler: { (alert: UIAlertAction!) -> Void in
print("from library")
//shows the photo library
self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .PhotoLibrary
self.imagePicker.modalPresentationStyle = .Popover
self.presentViewController(self.imagePicker, animated: true, completion: nil)
})
let cameraOption = UIAlertAction(title: "Take a photo", style: UIAlertActionStyle.Default, handler: { (alert: UIAlertAction!) -> Void in
print("take a photo")
//shows the camera
self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .Camera
self.imagePicker.modalPresentationStyle = .Popover
self.presentViewController(self.imagePicker, animated: true, completion: nil)
})
let cancelOption = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {
(alert: UIAlertAction!) -> Void in
print("Cancel")
self.dismissViewControllerAnimated(true, completion: nil)
})
//Adding the actions to the action sheet. Camera will only show up as an option if the camera is available in the first place.
optionMenu.addAction(photoLibraryOption)
optionMenu.addAction(cancelOption)
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) == true {
optionMenu.addAction(cameraOption)} else {
print ("I don't have a camera.")
}
self.presentViewController(optionMenu, animated: true, completion: nil)
/*
just adding extra text for fun
*/
}
// MARK: - Image Picker Delegates
//The UIImagePickerController is a view controller that gets presented modally. When we select or cancel the picker, it runs the delegate, where we handle the case and dismiss the modal.
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
print("finished picking image")
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//handle media here i.e. do stuff with photo
print("imagePickerController called")
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageButtonImage.image = chosenImage
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
//what happens when you cancel
//which, in our case, is just to get rid of the photo picker which pops up
dismissViewControllerAnimated(true, completion: nil)
}
The issue is that most of the times that I select an image the app crashes.I think it is partially due to some sort of low memory warning, although I am not sure. It is after I click the "use photo" button as shown below
Also, does anybody have any idea how I can remove the square thing whenever I take the photo as visible above
Thanks in advance!
Try using:
self.imagePicker.allowsEditing = false
I have an action sheet that has two options: one where you can choose an image from your library and one where you can take a photo from the camera. The photo library functions properly, but I can't seem to get the camera to work.
Here is my code:
let takePhoto = UIAlertAction(title: "Take photo", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
// present camera taker
var cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.sourceType = .Camera
self.presentViewController(cameraPicker, animated: true, completion: nil)
})
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
var selectedAvatar = UIImagePickerControllerOriginalImage
self.dismissViewControllerAnimated(false, completion: nil)
}
I can't seem to find the problem, can anybody help me out? The program crashes when I try to run it and click on Take Photo.
You are most likely running in the simulator. The simulator dont have a camera so to not make the app crash when pressing the button you have to check if cemera is available.
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
...
}
else {
print("Sorry cant take picture")
}
Follow the below code
func takePhoto()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
{
picker!.sourceType = UIImagePickerControllerSourceType.Camera
self .presentViewController(picker!, animated: true, completion: nil)
}
else
{
let alertWarning = UIAlertView(title:"Warning", message: "You don't have camera", delegate:nil, cancelButtonTitle:"OK", otherButtonTitles:"")
alertWarning.show()
}
}
func openGallary()
{
picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(picker!, animated: true, completion: nil)
}
//PickerView Delegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])
{
picker .dismissViewControllerAnimated(true, completion: nil)
imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
println("picker cancel.")
}
There is another reason why the camera can crash when used in a real device as Kirit Modi said in the following tread:
iOS 10 - App crashes To access photo library or device camera via UIImagePickerController
In iOS 10. You have to set privacy Setting for Camera & Photo Library. Camera & Photo Privacy Setting at info.Plist
This solved my crashing issue!