Mar*_*tin 5 iphone camera ios swift
我正在开发使用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")
}
}
Run Code Online (Sandbox Code Playgroud)
我使用以下代码打开相机.
presentViewController(imagePicker, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
我参考了以下链接和电话takePicture(),但似乎没有工作.
但我总是需要通过点击快门按钮手动拍摄照片.是否可以在Swift中以编程方式拍摄照片?
提前致谢.
1)检查视图控制器类中是否有这些代理:
UIImagePickerControllerDelegate, UINavigationControllerDelegate
Run Code Online (Sandbox Code Playgroud)
2)检查您的plist是否具有YES的权限
Privacy - Photo Library Usage Description
Privacy - Camera Usage Description
Run Code Online (Sandbox Code Playgroud)
如果你愿意,我使用这个代码(swift 3):
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
对于相机,只需更改"photoLibrary"
imagePicker.sourceType = .camera
Run Code Online (Sandbox Code Playgroud)
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)")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8859 次 |
| 最近记录: |