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
Related
I am having an issue while displaying camera captured image in an image view where delegates are set and being called "didFinishPickingMediaWithInfo".
I have two buttons for the user interaction (camera picture & from gallery), once I take picture from camera and then select any other image from gallery; the camera picture taken earlier is shown for a second. I tried searching a lot but no luck so far.
Can anyone please advise what am I missing. Following is the code for reference.
#IBAction func cameraButtonPressed(_ sender: UIButton) {
self.openCamera()
}
#IBAction func galleryButtonPressed(_ sender: UIButton) {
self.openGallary()
}
override func viewDidLoad() {
super.viewDidLoad()
initialLayout()
}
override func viewWillAppear(_ animated: Bool) {
self.lblPreview.isHidden = true
self.imageTake.isHidden = true
self.uploadButtonOutlet.isHidden = true
}
//
//MARK: - Internal Methods
func initialLayout() {
self.cameraButtonOutlet.layer.cornerRadius = 20
self.galleryButtonOutlet.layer.cornerRadius = 20
self.uploadButtonOutlet.layer.cornerRadius = 20
}
//MARK: - Open the camera
func openCamera(){
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)){
imagePicker.sourceType = UIImagePickerController.SourceType.camera
//If you dont want to edit the photo then you can set allowsEditing to false
imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.cameraCaptureMode = .photo
imagePicker.cameraDevice = .rear
self.present(imagePicker, animated: true, completion: nil)
}
else{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
//MARK: - Choose image from camera roll
func openGallary(){
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
imagePicker.allowsEditing = true
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
}
extension UploadTimesheetViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
self.lblPreview.isHidden = false
self.imageTake.isHidden = false
self.uploadButtonOutlet.isHidden = false
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
self.imageTake.image = editedImage
self.imageTake.setNeedsLayout()
})
// self.imageTake.image = editedImage
}
//Dismiss the UIImagePicker after selection
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.isNavigationBarHidden = false
self.dismiss(animated: true, completion: nil)
}
}
Update your delegate method like this,
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var finalImage:UIImage?
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
finalImage = image
}else {
finalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
}
self.lblPreview.isHidden = false
self.imageTake.isHidden = false
self.uploadButtonOutlet.isHidden = false
self.imageTake.image = finalImage
//Dismiss the UIImagePicker after selection
picker.dismiss(animated: true, completion: nil)
}
Sorry for the delayed response. Just updating the answer so if anyone is having the same issue should know what was the actual problem.
Once picker is dismissed a viewwillappear will be called where I have hid the image view and hence the reason it was not displaying it for the first time.
If I remove the code from viewwillappear and add that properties code to viewedload then all works fine.
The issue was once the picker is dismissed , viewillapear was called and that was creating the issue.
I am experimenting with UIImagePickerController to allow photos to be selected from either the library or by taking a photo with the camera.
I followed the steps on a website (https://makeapppie.com/2016/06/28/how-to-use-uiimagepickercontroller-for-a-camera-and-photo-library-in-swift-3-0/) and got this working for the photo library but whenever I try to invoke the camera from my app it gives the error "Thread 1: signal SIGABRT".
This is the code that I am using to invoke the camera:
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
It was my understanding that the SIGABRT error would be expected inside the simulator. However when I tried it on my iPhone 7 I expected it to work and it gave the same error.
I have added the "Privacy - Camera Usage Description" to the Info.plist file.
Any ideas what I have done wrong?
Here's my full code for using/selecting
// MARK: Camera App
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)
}
// MARK: Photos Albums
func showImagePicker() {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
//picker.modalPresentationStyle = .Popover
present(picker,
animated: true,
completion: nil)
picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
image = chosenImage
self.performSegue(withIdentifier: "ShowEditView", sender: self)
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: false, completion: nil)
}
// MARK: Seque to EditViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowEditView" {
if let vc = segue.destination as? EditViewController {
vc.image = image
//vc.image = images[0]
}
}
}
Ignore the two commented out lines - those are mine for testing things. This code works on all devices, iOS 9+, all orientations (but remember that iPhone is always displayed portrait), and I've never had any problems in either the simulator (it has no camera) nor a physical device.
Of interest is one thing that could cause an issue (not sure if it's throwing SIGABRT) - I'm checking for a rear camera and throwing up an alert if it doesn't exist. (No check for front camera though. I'm not even sure if anything but an iPod touch doesn't have a front facing camera.)
Also, don't forget to add two things to your info.plist for iOS 10:
<key>NSCameraUsageDescription</key>
<string>Used to capture new image for photo effect</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Used to select an image for photo effect</string>
You may put whatever you want in the description tags. Without these, the app will shut down in iOS 10 and Apple will reject your submission. Here's a link to more details.
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!
I am developing in Swift with iPhone camera.
I using the following code to check the camera:
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.allowsEditing = true
self.presentViewController(picker, animated: true)
} else {
print("can't find camera")
}
}
And I use the following code to open the camera.
presentViewController(imagePicker, animated: true, completion: nil)
I have reference the following link and call takePicture() , but it seems did not working.
But I always need to manually take the picture by tapping the shutter button. Is programmatically taking the picture possible in Swift?
Thanks in advance.
1) Check if you have these delegates in your view controller class:
UIImagePickerControllerDelegate, UINavigationControllerDelegate
2) Check if your plist have the permissions with YES
Privacy - Photo Library Usage Description
Privacy - Camera Usage Description
If you want, I use this code (swift 3):
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
For camera, just change "photoLibrary"
imagePicker.sourceType = .camera
3) In order to get the picture, you must implement the delegate didFinishPickingMediaWithInfo:
extension YourViewController:
UIImagePickerControllerDelegate,
UINavigationControllerDelegate
{
func imagePickerController(
_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any])
{
imagePicker.dismiss(animated: true, completion: nil)
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
print("picture taken: \(image)")
}
}
var imagePicker:UIImagePickerController = UIImagePickerController()
//Set delegate to imagePicker
imagePicker.delegate = self
//Allow user crop or not after take picture
imagePicker.allowsEditing = false
//set what you need to use "camera or photo library"
imagePicker.sourceType = .camera
//Switch flash camera
imagePicker.cameraFlashMode = .off
//Set camera Capture Mode photo or Video
imagePicker.cameraCaptureMode = .photo
//set camera front or rear
imagePicker.cameraDevice = .rear
//Present camera viewcontroller
self.present(imagePicker, animated: true, completion: nil)
and don't forget implement two protocol: UIImagePickerControllerDelegate,UINavigationControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let imagePicked = info[UIImagePickerControllerOriginalImage] as? UIImage {
UIImageWriteToSavedPhotosAlbum(imagePicked, nil, nil, nil)
dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
Because you want to take picture fully automatically, you don't need UIImagePickerController at all. Instead use AVFoundation directly.
This article will be very helpful: Camera Capture on iOS.
I try to write an App that needs a screen where you can take multible photos. I have used a code example from http://makeapppie.com/2015/11/04/how-to-make-xib-based-custom-uiimagepickercontroller-cameras-in-swift/.
It seems to be working OK, but my imagePickerController didFinishPickingMediaWithInfo newer get called. I am getting an error message from Xcode "Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates." It sounds to me like this could be the problem, and I have googled it, but havn't gotten any wiser. A lot of people write it's an Apple bug and I havn't found anybody offering a solution.
So do anybody know if it is the Xcode error that is my problem, and in that case have a solution for that or have I written something wrong in my code:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, CustomOverlayDelegate {
var picker = UIImagePickerController()
#IBAction func shootPhoto(sender: AnyObject) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker = UIImagePickerController() //make a clean controller
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
picker.showsCameraControls = false
//customView stuff
let customViewController = CustomOverlayViewController(
nibName:"CustomOverlayViewController",
bundle: nil
)
let customView:CustomOverlayView = customViewController.view as! CustomOverlayView
customView.frame = self.picker.view.frame
customView.cameraLabel.text = "Hello Cute Camera"
customView.delegate = self
//presentation of the camera
picker.modalPresentationStyle = .FullScreen
presentViewController(picker, animated: true,completion: {
self.picker.cameraOverlayView = customView
})
} else { //no camera found -- alert the user.
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)
presentViewController(
alertVC,
animated: true,
completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print("didFinishPickingMediaWithInfo")
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //get the image from info
UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil) //save to the photo library
}
//What to do if the image picker cancels.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
//MARK: Custom View Delegates
func didCancel(overlayView:CustomOverlayView) {
picker.dismissViewControllerAnimated(true, completion: nil)
print("dismissed!!")
}
func didShoot(overlayView:CustomOverlayView) {
picker.takePicture()
overlayView.cameraLabel.text = "Shot Photo"
print("Shot Photo")
}
func weAreDone(overlayView: CustomOverlayView) {
picker.dismissViewControllerAnimated(true,
completion: nil)
print("We are done!")
}
}
Write
picker.delegate = self after
picker = UIImagePickerController() line
Also inherit you class with UIImagePickerControllerDelegate delegate.
It will work.