美文网首页
swift3.0相机简单实用

swift3.0相机简单实用

作者: 超级疯小肛 | 来源:发表于2017-08-07 17:08 被阅读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相机简单实用

      本文链接:https://www.haomeiwen.com/subject/grcznttx.html