我处于相同的位置,并针对这个问题研究了两种不同的解决方案。
将 ViewController 设置为 AVCaptureVideoDataOutputSampleBufferDelegate 并对视频输出进行采样以截取屏幕截图。
将 ViewController 设置为 AVCapturePhotoCaptureDelegate 并捕获照片。
在这个问题中描述了设置前者的机制,例如:How to take UIImage of AVCaptureVideoPreviewLayer instead of AVCapturePhotoOutput capture
我同时实施了这两种方法来检查图像质量是否存在差异(没有差异)。
如果您只需要相机快照,那么就是这样。但听起来你需要在顶部绘制一个额外的动画。为此,我创建了一个与快照大小相同的容器 UIView,将 UIImageView 与快照一起添加到其中,然后在顶部绘制动画。之后,您可以在容器上使用 UIGraphicsGetImageFromCurrentImageContext 。
至于使用 (1) 和 (2) 中的哪一个,如果您不需要在应用程序中支持不同的摄像头方向,则可能无关紧要。但是,如果您需要在前后摄像头之间切换并支持不同的摄像头方向,那么您需要知道快照方向才能将动画应用到正确的位置,并且得到正确的结果证明是完全可以忍受的方法(1 )。
我使用的解决方案:
UIViewController 扩展了 AVCapturePhotoCaptureDelegate
将照片输出添加到 AVCaptureSession
private let session = AVCaptureSession()
private let photoOutput = AVCapturePhotoOutput()
....
// When configuring the session
if self.session.canAddOutput(self.photoOutput) {
self.session.addOutput(self.photoOutput)
self.photoOutput.isHighResolutionCaptureEnabled = true
}
- 捕获快照
let settings = AVCapturePhotoSettings()
let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
let previewFormat = [
kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
kCVPixelBufferWidthKey as String: 160,
kCVPixelBufferHeightKey as String: 160
]
settings.previewPhotoFormat = previewFormat
photoOutput.capturePhoto(with: settings, delegate: self)
- 先旋转或翻转快照,然后再进行其他操作
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
guard error == nil else {
// Do something
// return
}
if let dataImage = photo.fileDataRepresentation() {
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)
//https://developer.apple.com/documentation/uikit/uiimageorientation?language=objc
let orientation = UIApplication.shared.statusBarOrientation
var imageOrientation = UIImage.Orientation.right
switch orientation {
case .portrait:
imageOrientation = self.cameraPosition == .back ? UIImage.Orientation.right : UIImage.Orientation.leftMirrored
case .landscapeRight:
imageOrientation = self.cameraPosition == .back ? UIImage.Orientation.up : UIImage.Orientation.downMirrored
case .portraitUpsideDown:
imageOrientation = self.cameraPosition == .back ? UIImage.Orientation.left : UIImage.Orientation.rightMirrored
case .landscapeLeft:
imageOrientation = self.cameraPosition == .back ? UIImage.Orientation.down : UIImage.Orientation.upMirrored
case .unknown:
imageOrientation = self.cameraPosition == .back ? UIImage.Orientation.right : UIImage.Orientation.leftMirrored
@unknown default:
imageOrientation = self.cameraPosition == .back ? UIImage.Orientation.right : UIImage.Orientation.leftMirrored
}
let image = UIImage.init(cgImage: cgImageRef, scale: 1.0, orientation: imageOrientation)
// Do whatever you need to do with the image
} else {
// Handle error
}
}
如果您需要知道图像的大小来定位动画,您可以使用 AVCaptureVideoDataOutputSampleBufferDelegate 策略来检测缓冲区的大小一次。