1
我想拍攝照片,然後將其設置爲的UIImageView,從而創造了相機,我有以下代碼:捕獲圖像和設定它的UIImageView
class HomeController: BaseController, UIImagePickerControllerDelegate {
var detector: AFDXDetector?
var captureSession : AVCaptureSession?
var stillImageOutput : AVCapturePhotoOutput?
var previewLayer : AVCaptureVideoPreviewLayer?
var camera : AVCaptureDevice!
@IBOutlet weak var cameraBtn: UIButton!
@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var cameraImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
startCamera()
}
func startCamera() {
do {
captureSession = AVCaptureSession()
camera = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front)
captureSession?.sessionPreset = AVCaptureSessionPreset1280x720
let input = try AVCaptureDeviceInput(device: camera)
if (captureSession?.canAddInput(input))!{
captureSession?.addInput(input)
stillImageOutput = AVCapturePhotoOutput()
if (captureSession?.canAddOutput(stillImageOutput))!{
print("output added")
captureSession?.canAddOutput(stillImageOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
cameraView.layer.addSublayer(previewLayer!)
captureSession?.startRunning()
}
}
} catch {
}
}
@IBAction func cameraBtnPressed(_ sender: Any) {
if (stillImageOutput?.connection(withMediaType: AVMediaTypeVideo)) != nil
{
print("video connection detected")
}
}
}
出於某種原因,打印語句「視頻連接檢測「雖然相機正在工作,但不會被調用
還有其他人知道爲什麼嗎?