swift 使用AVCaptureEventInteraction进行音量按钮快门捕获

uurity8g  于 5个月前  发布在  Swift
关注(0)|答案(2)|浏览(44)

我已经被正式通知,iOS 17.2包含新的API,正式支持相机应用程序中用于照片捕捉的音量按钮驱动。
API称为AVCaptureEventInteractionLink
如果我使用AVCam sample code,我将如何附加此交互?

if #available(iOS 17.2, *) {
       let interaction = AVCaptureEventInteraction { event in
             print ("AVCaptureEventInteraction Fired")
       }
    }

字符串
API不包含任何详细信息。

nnvyjq4y

nnvyjq4y1#

AVCaptureEventInteraction实现了UIInterAction协议。因此,您应该将AVCaptureEventInteraction附加到响应器链的一部分的UI元素(* 例如 * 您的快门按钮)。
范例:

if #available(iOS 17.2, *) {
    let shutterEventInteraction = AVCaptureEventInteraction.init { event in
        print(event)
        if (event.phase == AVCaptureEventPhase.began) {
            self.performCapture()
        }
    }
    shutterEventInteraction.isEnabled = true
    self.shutterButton.addInteraction(shutterEventInteraction)
}

字符串

ahy6op9u

ahy6op9u2#

您需要将AVCaptureEventInteraction集成到AVCaptureSession或相关组件中。
API用法可能如下所示:

if #available(iOS 17.2, *) {
    let interaction = AVCaptureEventInteraction { event in
        // Handle the volume button press event here
        // e.g., Trigger photo capture
        print("AVCaptureEventInteraction Fired")
    }
    // Add the interaction to your capture session or related component
    // That might depend on further details from the API documentation
    captureSession.addInteraction(interaction)
}

字符串
但是,由于AVCaptureEventInteraction API文档并不详细,您可能需要试验在AVCaptureSession设置中的何处添加此交互。可能的位置是在初始化会话之后和启动会话之前。
我试了一个quick search on GitHub.
您可以尝试在设置会话之后但启动会话之前,在AVCam示例的configureSession方法中添加AVCaptureEventInteraction初始化。
AVCaptureEventInteraction的闭包中,实现按下音量按钮时捕获照片的逻辑。将交互添加到AVCaptureSession

func configureSession() {
    // existing setup code for AVCaptureSession 

    if #available(iOS 17.2, *) {
        let interaction = AVCaptureEventInteraction { event in
            // Implement the logic to capture a photo
            print("AVCaptureEventInteraction Fired")
            // Example: self.capturePhoto()
        }
        captureSession.addInteraction(interaction)
    }

    // rest of the session configuration 
}


self.capturePhoto()替换为AVCam中用于捕获照片的实际方法。

相关问题