为了开发一个应用程序,我设置了一个UIImagePicker,它的源类型是.savedPhotosAlbum
。
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)
}
现在我的应用程序已经完成,我想在我的Iphone上尝试我的应用程序,所以我更改了以下内容:
imagePicker.sourceType = .savedPhotosAlbum
到
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
我在我的
info.plist
中添加了<key>NSCameraUsageDescription</key>
<string>We want to access your camera to ...</string>
但当我试着拍照时,它总是给我看我的相册,这就是问题所在。
我想用手机的摄像头拍张照片。
最佳答案
你需要把这个密码
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.cameraCaptureMode = .photo
imagePicker.modalPresentationStyle = .fullScreen
在
viewDidLoad
或cameraTapped
中问题-现在您将此代码放在
UIImagePicker
的delegate方法中,该方法在选择图像或拍摄图像后调用。