SwiftUI AVCapturePhotoOutput не работает
У меня есть простое приложение SwiftUI с жизненным циклом SwiftUI, и я пытаюсь использовать AVFoundation для автоматического фотографирования. Я буду делать это на основе условий или на основе таймера, но в этом примере я просто хотел бы сделать снимок при запуске и отобразить его (НЕ слой предварительного просмотра). Я не хочу, чтобы от пользователя требовалось никаких действий.
Я явно не понимаю, как правильно настроить и захватить.
import SwiftUI
import AVFoundation
struct ContentView: View {
let dataStore = DataStore.shared
@State private var captureSession = AVCaptureSession()
@State private var backCamera : AVCaptureDevice?
@State private var frontCamera : AVCaptureDevice?
@State private var currentCamera : AVCaptureDevice?
@State private var photoOutput : AVCapturePhotoOutput?
@State private var capturedImage: UIImage?
var body: some View {
VStack {
Text("Take a Photo Automatically")
.padding()
ZStack {
RoundedRectangle(cornerRadius: 0)
.stroke(Color.blue, lineWidth: 4)
.frame(width: 320, height: 240, alignment: .center)
Image(uiImage: dataStore.capturedImage)
}
Spacer()
}
.onAppear {
if UIImagePickerController.isSourceTypeAvailable(.camera){
self.setupCaptureSession()
self.setupDevices()
self.setupInputOutput()
self.startRunningCaptureSession()
} else {
print("No Camera is Available")
}
}
}
func setupCaptureSession() {
captureSession.sessionPreset = AVCaptureSession.Preset.photo
}//setupCaptureSession
func setupDevices() {
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: .video, position: .unspecified)
let devices = deviceDiscoverySession.devices
for device in devices {
if device.position == AVCaptureDevice.Position.back {
backCamera = device
} else if device.position == AVCaptureDevice.Position.front {
frontCamera = device
}//if else
}//for in
currentCamera = frontCamera
}//setupDevices
func setupInputOutput() {
do {
//you only get here if there is a camera ( ! ok )
let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
captureSession.addInput(captureDeviceInput)
photoOutput = AVCapturePhotoOutput()
photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: {(success, error) in
})
captureSession.addOutput(photoOutput!)
captureSession.commitConfiguration()
} catch {
print("Error creating AVCaptureDeviceInput:", error)
}
}//setupInputOutput
func startRunningCaptureSession() {
let settings = AVCapturePhotoSettings()
captureSession.startRunning()
photoOutput?.capturePhoto(with: settings, delegate: PhotoDelegate())
}//startRunningCaptureSession
}//struct
class PhotoDelegate: NSObject, AVCapturePhotoCaptureDelegate {
let dataStore = DataStore.shared
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
guard let data = photo.fileDataRepresentation(),
let image = UIImage(data: data) else {
return
}
dataStore.capturedImage = image
}
}//photo delegate
class DataStore {
static let shared = DataStore()
private init() {}
@Published var capturedImage: UIImage = UIImage()
}//dataStore
Любое руководство будет оценено. Xcode 12.5.1 iOS 14.5
1 ответ
Основная проблема в том, что вы создаете
PhotoDelegate
но не храните его. В iOS
delegate
объект обычно хранится как слабая ссылка, чтобы предотвратить цикл циклической ссылки / сохранения.
Вы можете исправить это, просто создав другое свойство в вашем представлении, но вместо этого я предлагаю вам создать класс модели. Если вы делаете что-то, не связанное с самим представлением, это признак того, что вам лучше переместить его в другое место, например ObservableObject. Вы также можете сделать его своим делегатом, поэтому вам не нужно создавать отдельный объект и использовать синглтон: это еще один признак того, что вы делаете что-то не так.
class CaptureModel: NSObject, ObservableObject {
let captureSession = AVCaptureSession()
var backCamera: AVCaptureDevice?
var frontCamera: AVCaptureDevice?
var photoOutput: AVCapturePhotoOutput?
var currentCamera: AVCaptureDevice?
@Published
var capturedImage: UIImage?
override init() {
super.init()
setupCaptureSession()
setupDevices()
setupInputOutput()
}
func setupCaptureSession() {
captureSession.sessionPreset = AVCaptureSession.Preset.photo
}//setupCaptureSession
func setupDevices() {
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: .video, position: .unspecified)
let devices = deviceDiscoverySession.devices
for device in devices {
if device.position == AVCaptureDevice.Position.back {
backCamera = device
} else if device.position == AVCaptureDevice.Position.front {
frontCamera = device
}//if else
}//for in
currentCamera = frontCamera
}//setupDevices
func setupInputOutput() {
do {
//you only get here if there is a camera ( ! ok )
let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
captureSession.addInput(captureDeviceInput)
photoOutput = AVCapturePhotoOutput()
photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: {(success, error) in
})
captureSession.addOutput(photoOutput!)
captureSession.commitConfiguration()
} catch {
print("Error creating AVCaptureDeviceInput:", error)
}
}//setupInputOutput
func startRunningCaptureSession() {
let settings = AVCapturePhotoSettings()
captureSession.startRunning()
photoOutput?.capturePhoto(with: settings, delegate: self)
}//startRunningCaptureSession
func stopRunningCaptureSession() {
captureSession.stopRunning()
}//startRunningCaptureSession
}
extension CaptureModel: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
guard let data = photo.fileDataRepresentation(),
let image = UIImage(data: data) else {
return
}
capturedImage = image
}
}
struct ContentView: View {
@StateObject
var model = CaptureModel()
var body: some View {
VStack {
Text("Take a Photo Automatically")
.padding()
ZStack {
RoundedRectangle(cornerRadius: 0)
.stroke(Color.blue, lineWidth: 4)
.frame(width: 320, height: 240, alignment: .center)
model.capturedImage.map { capturedImage in
Image(uiImage: capturedImage)
}
}
Spacer()
}
.onAppear {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
model.startRunningCaptureSession()
} else {
print("No Camera is Available")
}
}
.onDisappear {
model.stopRunningCaptureSession()
}
}
}//struct