大家好,我已经在我的应用程序中实现了一个相机屏幕。
我正在使用func photoOutput
通过在我的IBAction插座中调用来捕获我的实时预览。
与此同时,我设法查找如何使用AVVideo将CIFilters应用于我的实时预览
但问题是我的实时预览现在在视频会话上运行而不是在我实施过滤器后的照片会话
我无法捕捉到我看到的画面。
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer:
CMSampleBuffer, from connection: AVCaptureConnection) {
connection.videoOrientation = orientation
let videoOutput = AVCaptureVideoDataOutput()
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main)
let comicEffect = CIFilter(name: "CIPhotoEffectMono")
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let cameraImage = CIImage(cvImageBuffer: pixelBuffer!)
comicEffect!.setValue(cameraImage, forKey: kCIInputImageKey)
let cgImage = self.context.createCGImage(comicEffect!.outputImage!, from: cameraImage.extent)!
DispatchQueue.main.async {
let filteredImage = UIImage(cgImage: cgImage)
self.captureImageView.image = filteredImage
}
是我的过滤器代码,我的实时屏幕捕获代码如下
func photoOutput(_ captureOutput: AVCapturePhotoOutput,
didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?,
previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?,
resolvedSettings: AVCaptureResolvedPhotoSettings,
bracketSettings: AVCaptureBracketedStillImageSettings?,
error: Error?) {
// Make sure we get some photo sample buffer
guard error == nil,
let photoSampleBuffer = photoSampleBuffer else {
print("Error capturing photo: \(String(describing: error))")
return
}
// Convert photo same buffer to a jpeg image data by using AVCapturePhotoOutput
guard let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
return
}
// Initialise an UIImage with taken image data
let capturedImage = UIImage.init(data: imageData , scale: 1.0)
if let image = capturedImage{
// Save captured image to photos album
// Resize image taken to the ratio of the previewView
let originalImage = image
let liveImage: CGRect = previewView.frame
let liveRatio = (liveImage.width) / (liveImage.height)
let resultingRectWidth = originalImage.size.width
let resultingRectHeight = resultingRectWidth / (liveRatio)
let resultingRect: CGRect = CGRect(x: 0, y: 0, width: resultingRectWidth, height: resultingRectHeight)
// print("@#$@#$@#$#@$@#$")
// print(originalImage.size.width)
// print(originalImage.size.height)
// print(resultingRectWidth)
// print(resultingRectHeight)
// print(liveImage.size.width)
// print(liveImage.size.height)
// print("@#$#@$@#$#$@#$@#")
let topInset = -(originalImage.size.height - resultingRectHeight) / 2
let croppedImage = originalImage.inAppCrop(image: originalImage, cropRect: resultingRect, offset: topInset)
captureImageView.image = croppedImage
UIImageWriteToSavedPhotosAlbum(croppedImage!, nil, nil, nil)
captureImageView.isHidden = false
captureButtonView.isHidden = true
captureButton.isHidden = true
selfieButton.isHidden = true
self.uploadPageViewController?.disablePaging()
self.uploadTabBarViewController?.inAppCamButton.isUserInteractionEnabled = false
self.uploadTabBarViewController?.libraryButton.isUserInteractionEnabled = false
self.uploadTabBarViewController?.inAppCamImage = croppedImage
}
}
我的IBaction调用photoOutput函数如下
@IBAction func onTapTakePhoto(_ sender: Any) {
// Make sure capturePhotoOutput is valid
guard let capturePhotoOutput = self.capturePhotoOutput else { return }
// Get an instance of AVCapturePhotoSettings class
let photoSettings = AVCapturePhotoSettings()
// Set photo settings for our need
photoSettings.isAutoStillImageStabilizationEnabled = true
photoSettings.isHighResolutionPhotoEnabled = true
photoSettings.flashMode = .off
// Call capturePhoto method by passing our photo settings and a delegate implementing AVCapturePhotoCaptureDelegate
capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self)
self.uploadTabBarViewController?.titleButton.setTitle("Preview", for: .normal)
self.uploadTabBarViewController?.previewCheckButton.isHidden = false
self.uploadTabBarViewController?.backButton.isHidden = false
self.uploadTabBarViewController?.cancelButton.isHidden = true
}