在使用的的时候,遇到"No active and enabled video connection" 崩溃了,后来发现是session.addOutput(output)没有添加进去。
reason: '*** -[AVCapturePhotoOutput capturePhotoWithSettings:delegate:] No active and enabled video connection'
//
// Created by administrator on 2019/4/20.
// Copyright © 2019 . All rights reserved.
import UIKit
import AVFoundation
class PhotoVC: UIViewController,AVCapturePhotoCaptureDelegate{
var player:AVCaptureVideoPreviewLayer!
let session = AVCaptureSession()
let output = AVCapturePhotoOutput()
var input:AVCaptureDeviceInput!
var device:AVCaptureDevice!
@IBOutlet weak var imgView:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
createCamera()
}
private func createCamera() {
session.beginConfiguration()
session.sessionPreset = AVCaptureSession.Preset.photo
device = AVCaptureDevice.default(for: .video)
session.automaticallyConfiguresCaptureDeviceForWideColor = true
guard device != nil else { return }
do{
input = try AVCaptureDeviceInput.init(device: device!)
if session.canAddInput(input) {
session.addInput(input)
}else {
session.commitConfiguration()
return
}
if session.canAddOutput(output) {
session.addOutput(output)
//崩溃的原因是这里写错了session.addOutput(output) 写成了 session.canAddOutput(output)
output.isHighResolutionCaptureEnabled = true
}else {
session.commitConfiguration()
return
}
player = AVCaptureVideoPreviewLayer.init(session: session)
player.frame = CGRM(0, 0, 160, 100)
player.videoGravity = .resizeAspectFill
self.view.layer.addSublayer(player)
session.commitConfiguration()
session.startRunning()
}
catch let error as NSError {
print("---出错了----\(error)")
}
}
///拍照
@IBAction func onClickBtn(_ sender: UIButton) {
let settings = AVCapturePhotoSettings()
if settings.availablePreviewPhotoPixelFormatTypes.count > 0 {
settings.previewPhotoFormat = [ kCVPixelBufferPixelFormatTypeKey as String : settings.availablePreviewPhotoPixelFormatTypes.first!]
}
output.capturePhoto(with: settings, delegate: self)
}
///获取到照片
func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
if let error = error {
print("-----error occure : \(error.localizedDescription)")
}
if let sampleBuffer = photoSampleBuffer,
let previewBuffer = previewPhotoSampleBuffer,
let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {
print(UIImage(data: dataImage)?.size as Any)
let dataProvider = CGDataProvider(data: dataImage as CFData)
let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)
self.session.stopRunning()
self.imgView.image = image
} else {
print("some error here")
}
}
}
有什么错误欢迎批评指正 。
网友评论