iOS相机应用Swift 3



我想要一个带有捕获和保存图像功能的应用程序中的相机界面在ViewController上。

class ViewController: UIViewController , UINavigationControllerDelegate, UIImagePickerControllerDelegate{
@IBOutlet var imageView: UIImageView!

@IBOutlet var TakePhoto: UIButton!
var imagePick: UIImagePickerController!
var newMedia: Bool?
@IBAction func TakePhoto(_ sender: AnyObject){
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
        let imagePick = UIImagePickerController()
        imagePick.delegate = self
        imagePick.sourceType = UIImagePickerControllerSourceType.camera
        imagePick.mediaTypes = [kUTTypeImage as String]
        imagePick.allowsEditing = false
        self.present(imagePick, animated: true, completion: nil)
        newMedia = true
    }
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let mediaType = info[UIImagePickerControllerMediaType] as! NSString
    if mediaType.isEqual(to: kUTTypeImage as String){
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageView.image = image
        if(newMedia == true){
           UIImageWriteToSavedPhotosAlbum(image, self, #selector(ViewController.image(image:didFinishSavingWithError:contextInfo:)), nil)
        } //else if mediaType.isEqual(to: kUTTypeMovie as String)
        self.dismiss(animated: true, completion: nil)
    }
}
func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer){
    if error != nil{
        let alert = UIAlertController(title: "Save Failed", message: "Failed to save the image", preferredStyle: UIAlertControllerStyle.alert)
        let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alert.addAction(cancelAction)
        self.present(alert, animated: true, completion: nil)
    }
}

}

您是否设置了在info.plist中使用相机的权限?

我的意思是放置密钥隐私 - 摄像机用法描述,用描述键入字符串,为什么要使用

最新更新