Configurez une caméra personnalisée à l'aide de AVCapturePhotoOutput. Configuration de l'AVCapturePhotoOutput pour fournir un tampon d'aperçu (vignette) en plus du tampon JPEG principal.AVCapturePhotoOutput ne fournit pas de tampon d'aperçu
Le problème est que je ne reçois le tampon de prévisualisation qu'une seule fois (première capture) et que je reçois ensuite une seconde fois (le photoSampleBuffer principal est toujours reçu correctement).
Voilà comment je configurer la capture:
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)
}
}
Dans mon PhotoCaptureDelegate (qui met en œuvre 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)
}
}
}
Ce qui se passe est que la première fois que je capture que je reçois à la fois la photoSampleBuffer & previewPhotoSampleBuffer. 2ème fois et je ne reçois photoSampleBuffer
et previewPhotoSampleBuffer = nil
bien que quand je vérifie resolvedSettings.previewDimensions
je reçois: CMVideoDimensions(width: 160, height: 120)
Si je passe la caméra (avant vers l'arrière) par une nouvelle configuration de la session de capture la première capture est ensuite sur OK, puis à nouveau pas aperçu tampon. le paramètre error
dans les rappels de délégué est toujours nul.
testé sur iPhone 6 fonctionnant sous iOS 10.3.1