如何使用swift(使用按钮切换)在GPUImage2(Switch版本)中旋转或切换前后摄像头。任何帮助将非常感谢。谢谢你提前。
让我们考虑GPUImage2的主要示例:
do {
camera = try Camera(sessionPreset:AVCaptureSessionPreset640x480)
filter = SaturationAdjustment()
camera --> filter --> renderView
camera.startCapture()
} catch {
fatalError("Could not initialize rendering pipeline: \(error)")
}
相机开始拍摄后,您可以通过以下方式从后向前切换:
do {
// Stop the capture of the current camera and remove its targets
sharedImageProcessingContext.runOperationSynchronously{
camera.stopCapture()
camera.removeAllTargets()
}
// Build a new Camera object, using the back or front physical camera
if camera.location == .frontFacing {
camera = try Camera(sessionPreset:.vga640x480, location:.backFacing)
} else {
camear = try Camera(sessionPreset:.vga640x480, location:.frontFacing)
}
//Rebuild the pipeline
camera --> filter --> renderView
camera.startCapture()
} catch {
print("Error switching camera : \(error)")
}
侧注#1:默认摄像头使用的是默认摄像头,默认值为Camera.init():
public init(sessionPreset:AVCaptureSession.Preset,
cameraDevice:AVCaptureDevice? = nil,
location:PhysicalCameraLocation = .backFacing,
...
附注2:Camera对象显示location
属性。不幸的是,它的didSet
访问器仍在“TODO”中:
public var location:PhysicalCameraLocation {
didSet {
// TODO: Swap the camera locations, framebuffers as needed
}
}
我找到了答案。
我再次重新加载我的视图控制器,然后通过bool值检查它,并相应地初始化摄像机,即渲染视图。