Swift 学习笔记(四)
2019-01-22
这次尝试了调用摄像头 //原来iOS Simulators 是没有摄像头的啊 //于是用真机调试…
文件 ViewController.swift
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func buttonOne(_ sender: Any) {
print("touch button")
let imagePickerController: UIImagePickerController = UIImagePickerController()
//print(UIImagePickerController.isSourceTypeAvailable(.camera))//可以用if判断有没有摄像头
imagePickerController.modalTransitionStyle = .flipHorizontal
imagePickerController.allowsEditing = true//允许编辑
imagePickerController.sourceType = .camera//数据来源为摄像头
imagePickerController.videoMaximumDuration = 15 //录制视频长度/秒
imagePickerController.mediaTypes = ["public.image","public.movie"] //允许的相机类型
imagePickerController.videoQuality = .typeHigh//视频质量
imagePickerController.cameraCaptureMode = .photo //初始摄像头模式/拍照 摄像
self.present(imagePickerController, animated: true){
() -> Void in
print("ok")
}
}
}
其中用到摄像头,要到 Info.plist
中加上 Privacy - Camera Usage Description
向用户请求摄像头权限
如果是录制视频还需要加上 Privacy - Microphone Usage Description
向用户请求录制视频声音
没有以上两句会导致程序崩溃 并在命令框里显示错误信息
- 参考资料 : 参考资料