user2462805 user2462805 - 25 days ago 7
Swift Question

UIImagePicker source type doesn't want to change

I've set an UIImagePicker with a

.savedPhotosAlbum
as a sourcetype in order to develop an app.

import UIKit

class PictureViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

/////////////////// OUTLETS ////////////////////
///////////////// PROPERTIES ///////////////////
var imagePicker = UIImagePickerController()

override func viewDidLoad() {
super.viewDidLoad()

imagePicker.delegate = self

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// Where do we want our photos from and we don't want edited photos
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.cameraCaptureMode = .photo
imagePicker.modalPresentationStyle = .fullScreen

// Let's take the image from the picker
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
snapImage.image = image

// When the user picks a picture, disabling the background color of the UIImageView
snapImage.backgroundColor = UIColor.clear

// Closing the image picker
imagePicker.dismiss(animated: true, completion: nil)
}

@IBAction func cameraTapped(_ sender: Any) {
present(imagePicker, animated: true, completion: nil)
}


Now that my app is finished I want to try my app on my Iphone so I changed this :

imagePicker.sourceType = .savedPhotosAlbum


to

imagePicker.sourceType = UIImagePickerControllerSourceType.camera


And I have added in my
info.plist


<key>NSCameraUsageDescription</key>
<string>We want to access your camera to ...</string>


But when I try to take a picture, it keeps showing me my photos album and that is the problem.
I want to be able to take a picture from the camera of my phone.

Answer

You need to put this code

imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.cameraCaptureMode = .photo
imagePicker.modalPresentationStyle = .fullScreen

in either viewDidLoad or cameraTapped

Problem - As right now you are putting this code in the delegate method of UIImagePicker which gets called after either picking the image or taking the image.

Comments