如何通过Swift编程自动拍照?

时间:2016-04-26 10:19:43

标签: ios iphone swift camera

我正在使用iPhone相机在Swift中开发。

我使用以下代码检查相机:

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")
    }
}

我使用以下代码打开相机。

presentViewController(imagePicker, animated: true, completion: nil)

我参考了以下link并致电takePicture(),但似乎无效。

但我总是需要通过点击快门按钮手动拍摄照片。是否可以在Swift中以编程方式拍摄照片?

提前致谢。

3 个答案:

答案 0 :(得分:6)

1)检查视图控制器类中是否有这些代理:

 UIImagePickerControllerDelegate, UINavigationControllerDelegate

2)检查您的plist是否具有YES

的权限
Privacy - Photo Library Usage Description
Privacy - Camera Usage Description

如果你愿意,我使用这个代码(swift 3):

    let imagePicker = UIImagePickerController()
    imagePicker.sourceType = .photoLibrary
    imagePicker.delegate =  self
    self.present(imagePicker, animated: true, completion: nil)

对于相机,只需更改" photoLibrary"

    imagePicker.sourceType = .camera

3)为了获得图片,您必须实现委托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)")
  }
}

答案 1 :(得分:4)

    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) 

并且不要忘记实现两个协议: 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)
}

答案 2 :(得分:0)

因为您想要完全自动拍照,所以根本不需要f.input :user_id, as: :search_select, url: admin_users_path, fields: [:user_last_name], display_name: 'name', minimum_input_length: 2 。而是直接使用AVFoundation。

本文非常有用:Camera Capture on iOS