访问和使用IOS摄像头
我正在努力学习如何在IOS设备上访问和使用摄像头,事实上我找不到那么多关于它的信息,至少不是最新的,或者对初学者来说很有用 不管怎样,我已经尝试了下面的代码,但是当我点击按钮打开相机时什么都没有发生,它只是冻结了,什么也不做,我是错过了什么还是做错了什么访问和使用IOS摄像头,ios,swift,camera,uiimagepickercontroller,Ios,Swift,Camera,Uiimagepickercontroller,我正在努力学习如何在IOS设备上访问和使用摄像头,事实上我找不到那么多关于它的信息,至少不是最新的,或者对初学者来说很有用 不管怎样,我已经尝试了下面的代码,但是当我点击按钮打开相机时什么都没有发生,它只是冻结了,什么也不做,我是错过了什么还是做错了什么 import UIKit import MobileCoreServices class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigat
import UIKit
import MobileCoreServices
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var myImg: UIImageView!
var newMedia: Bool!
@IBAction func cameraRoll(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
newMedia = false
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
self.dismiss(animated: true, completion: nil)
if mediaType.isEqual(to: kUTTypeImage as String){
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
myImg.image = image
if (newMedia == true) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(ViewController.image(image:didFinishsavingWithError:contextInfo:)), nil)
} else if mediaType.isEqual(to: kUTTypeMovie as String){
//vid support
}
}
}
func image(image: UIImage, didFinishsavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer){
if error != nil {
let alert = UIAlertController(title: "Save Failed", message: "Failed to save image", preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
@IBAction func takePhoto(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
newMedia = false
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
在您的
拍照
方法中,我假设您希望使用该方法访问您的相机,您正在将imagePicker源设置为.photoLibrary
。您是否通过添加NSCameraUsageDescription
和NSPhotoLibraryUsageDescription
密钥以及对每种密钥的解释来实现安全协议?如果不是,它可能会导致崩溃。这就是我所做的,我正在遵循一个教程。您正在检查源类型camera是否可用,然后在下面的代码中,您将sourceType设置为photoLibrary,将其更改为UIImagePickerControllerSourceType.camera。还有一件事,请在“设置”中检查应用程序的摄像头权限。您是在模拟器上还是在实际设备上进行尝试?因为功能相关的摄像头在模拟器上无法工作。可能重复