gpt4 book ai didi

ios - 如何用Swift编程实现自动拍照?

转载 作者:搜寻专家 更新时间:2023-10-31 08:15:21 26 4
gpt4 key购买 nike

我正在使用 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 中以编程方式拍摄照片是否可行?

提前致谢。

最佳答案

1) 检查你的 View Controller 类中是否有这些委托(delegate):

 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) 为了获取图片,必须实现委托(delegate)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)")
}
}

关于ios - 如何用Swift编程实现自动拍照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36862051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com