使用AVCapturePhotoOutput設置自定義攝像頭。 配置AVCapturePhotoOutput以提供除主JPEG緩衝區之外的預覽緩衝區(縮略圖)。AVCapturePhotoOutput不提供預覽緩衝區
問題是我只接收預覽緩衝區一次(第一次捕捉),然後從第二次接收零(主photoSampleBuffer總是正確接收)。
下面是我設置捕獲:
func capturePhoto() {
guard let videoPreviewLayerOrientation = deviceOrientation.videoOrientation else { return }
sessionQueue.async {
if let photoOutputConnection = self.photoOutput.connection(withMediaType: AVMediaTypeVideo) {
photoOutputConnection.videoOrientation = videoPreviewLayerOrientation
}
// each photo captured requires a brand new setting object and capture delegate
let photoSettings = AVCapturePhotoSettings()
// Capture a JPEG photo with flash set to auto and high resolution photo enabled.
photoSettings.isHighResolutionPhotoEnabled = true
//configure to receive a preview image (thumbnail)
if let previewPixelType = photoSettings.availablePreviewPhotoPixelFormatTypes.first {
let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String : previewPixelType,
kCVPixelBufferWidthKey as String : NSNumber(value: 160),
kCVPixelBufferHeightKey as String : NSNumber(value: 160)]
photoSettings.previewPhotoFormat = previewFormat
}
// TODO: photoSettings.flashMode = .auto
// Use a separate object for the photo capture delegate to isolate each capture life cycle.
let photoCaptureDelegate = PhotoCaptureDelegate(with: photoSettings, willCapturePhotoAnimation: { [unowned self] in
// show shutter animation
self.shutterAnimation()
}, completed: { [unowned self] (photoCaptureDelegate, photoData, previewThumbnail) in
self.captureCompleted(photoCaptureDelegate: photoCaptureDelegate, data: photoData, thumbnail: previewThumbnail)
}
)
// The Photo Output keeps a weak reference to the photo capture delegate so we store it in an array
// to maintain a strong reference to this object until the capture is completed.
self.inProgressPhotoCaptureDelegates[photoCaptureDelegate.requestedPhotoSettings.uniqueID] = photoCaptureDelegate
self.photoOutput.capturePhoto(with: photoSettings, delegate: photoCaptureDelegate)
}
}
在我PhotoCaptureDelegate(實現AVCapturePhotoCaptureDelegate):
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
if let photoBuffer = photoSampleBuffer {
photoData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoBuffer, previewPhotoSampleBuffer: nil)
}
if let previewBuffer = previewPhotoSampleBuffer {
if let pixelBuffer = CMSampleBufferGetImageBuffer(previewBuffer) {
photoThumbnail = CIImage(cvPixelBuffer: pixelBuffer)
}
}
}
發生什麼事是我第一次拍攝我收到兩個photoSampleBuffer & previewPhotoSampleBuffer。第2次和我只收到photoSampleBuffer
和previewPhotoSampleBuffer = nil
雖然當我檢查resolvedSettings.previewDimensions
我得到: CMVideoDimensions(width: 160, height: 120)
如果我通過重新配置捕獲會話第一次捕捉之後是確定,然後再次沒有預覽切換攝像頭(前後)緩衝。委託回調中的參數error
始終爲零。
在測試了iPhone 6運行iOS 10.3.1