swift3.0相机简单实用

//MARK:-相机

letimagePicker =UIImagePickerController()

//检测相机是否可用

letisAvailable =UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)

//必须首先设置sourceType然后再设置其他属性否则会出异常

imagePicker.sourceType= .photoLibrary

ifisAvailable {

imagePicker.sourceType= .camera

//如果有前置摄像头则调用前置摄像头

imagePicker.cameraDevice=UIImagePickerControllerCameraDevice.front

////是否显示控制栏,这里选择false会展示系统相机UI,如果要自定义可以将其隐藏,这里使用系统相机

imagePicker.showsCameraControls=false

}

imagePicker.delegate=self

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

imagePicker.allowsEditing=true

//这里会用到 UIImagePickerControllerDelegate,UINavigationControllerDelegate ,这两个代理

//MARK: -相机代理回调方法

//获取相机拍的图片

funcimagePickerController(_picker:UIImagePickerController, didFinishPickingMediaWithInfo info: [String:Any]) {

dismiss(animated:true, completion:nil)

ifletphoto = info[UIImagePickerControllerOriginalImage]as!UIImage?{

referenceImage= photo

referenceImageBtn.setImage(photo, for:UIControlState.normal)

}

}

//点击取消的回调

funcimagePickerControllerDidCancel(_picker:UIImagePickerController) {

dismiss(animated:true, completion:nil)

}

你可能感兴趣的:(swift3.0相机简单实用)