我正在尝试使用swift 4在ios中创建pip效果相机,为此我采取了以下控件: 视图 FrontCameraView(ImageView的) BackCameraView(ImageView的) MaskedCameraView(ImageView的)
FrontCameraView采用模糊图像,帧图像采用pip帧,maskedCameraView采用蒙版图像。
现在,当我捕捉图像时
这是为了在自定义相机上应用实时效果: -
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
connection.videoOrientation = orientation
let videoOutput = AVCaptureVideoDataOutput()
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main)
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let cameraImage = CIImage(cvImageBuffer: pixelBuffer!)
let cgImage:CGImage = context.createCGImage(cameraImage, from: cameraImage.extent)!
globalImage = UIImage.init(cgImage: cgImage)
let croppedImage = HelperClass.shared().imageByScalingAndCropping(forSize:globalImage, CGSize.init(width:200, height: 200))
DispatchQueue.main.async {
self.backCameraView?.image = HelperClass.shared().blur(withCoreImage:croppedImage, andView:self.view)
self.frontCameraView?.image = frameArray[self.tagValue]
let maskedImage = self.maskImage(image:(croppedImage)!, mask:maskedArray[self.tagValue])
let maskedcroppedImage = HelperClass.shared().imageByScalingAndCropping(forSize:maskedImage, CGSize.init(width:200, height: 200))
self.maskedCameraView.image = maskedcroppedImage
}
}
//用于捕获图像的代码 @IBAction func takePhoto(_ sender:UIButton){
if(captureSession.canAddOutput(videoOutput)){
captureSession.addOutput(videoOutput)
}
let videoConnection = videoOutput.connection(with: AVMediaType.video)
if(videoConnection != nil){
photoOutput?.captureStillImageAsynchronously(from:videoConnection!, completionHandler: { (sampleBuffer, error) in
if sampleBuffer != nil{
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer!)
let dataProvider = CGDataProvider.init(data:imageData! as CFData)
let cgImageRef = CGImage.init(jpegDataProviderSource:dataProvider!, decode:nil, shouldInterpolate: true, intent: CGColorRenderingIntent.relativeColorimetric)
let image = UIImage.init(cgImage: cgImageRef!, scale:1.0, orientation:UIImageOrientation.right)
let sourceImage = HelperClass.shared().imageByScalingAndCropping(forSize:image,CGSize.init(width:200, height:200))
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let framePreviewVC = storyBoard.instantiateViewController(withIdentifier: "FramePreviewController") as! FramePreviewController; framePreviewVC.frameImage = sourceImage!
self.navigationController?.pushViewController(framePreviewVC, animated: true)
}
})
}
}
问题: - 我能够使用此代码实现PIPeffect,但是当我尝试使用videoOutput(AVCaptureVideoDataOutput)捕获图像时,它无法使我能够处理图像。 如果我使用photoOutput(AVCaptureStillImageOutput)做同样的事情,它不会让我在现场摄像机上应用PIP效果。请帮我解决这个问题,我已经坚持这一点一个星期了。 任何形式的指导都会受到高度赞赏。谢谢!