react-native-vision-camera/ios/PhotoCaptureDelegate.swift
Marc Rousavy 71730a73ef
fix: Fix AVAudioSession not allowing background music playback (#155)
* Set category always if different

* rename org

* Fix video format sorting

* fix format filtering

* Update AVAudioSession+setCategoryIfNotSet.swift

* upgrade all dependencies

* Also run dependabot for JS codebase

* Update MediaPage.tsx

* Use typescript 4.2.4

* Also run TS in check-all

* Downgrade typescript to 4.2.3

* f

* recreate lockfiles

* docs: Revert package.json changes

* revert all package.json changes

* Update Podfile.lock

* bump all dependencies, pin typescript to 4.2.4

* Downgrade react-native-navigation for now

* upgrade to later snapshot

* Update yarn.lock

* remove yeet
2021-06-01 13:07:57 +02:00

74 lines
2.1 KiB
Swift

//
// PhotoCaptureDelegate.swift
// Cuvent
//
// Created by Marc Rousavy on 15.12.20.
// Copyright © 2020 mrousavy. All rights reserved.
//
import AVFoundation
private var delegatesReferences: [NSObject] = []
// MARK: - PhotoCaptureDelegate
class PhotoCaptureDelegate: NSObject, AVCapturePhotoCaptureDelegate {
required init(promise: Promise) {
self.promise = promise
super.init()
delegatesReferences.append(self)
}
func photoOutput(_: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
defer {
delegatesReferences.removeAll(where: { $0 == self })
}
if let error = error as NSError? {
return promise.reject(error: .capture(.unknown(message: error.description)), cause: error)
}
let error = ErrorPointer(nilLiteral: ())
guard let tempFilePath = RCTTempFilePath("jpeg", error)
else {
return promise.reject(error: .capture(.createTempFileError), cause: error?.pointee)
}
let url = URL(string: "file://\(tempFilePath)")!
guard let data = photo.fileDataRepresentation()
else {
return promise.reject(error: .capture(.fileError))
}
do {
try data.write(to: url)
let exif = photo.metadata["{Exif}"] as? [String: Any]
let width = exif?["PixelXDimension"]
let height = exif?["PixelYDimension"]
return promise.resolve([
"path": tempFilePath,
"width": width as Any,
"height": height as Any,
"isRawPhoto": photo.isRawPhoto,
"metadata": photo.metadata,
"thumbnail": photo.embeddedThumbnailPhotoFormat as Any,
])
} catch {
return promise.reject(error: .capture(.fileError), cause: error as NSError)
}
}
func photoOutput(_: AVCapturePhotoOutput, didFinishCaptureFor _: AVCaptureResolvedPhotoSettings, error: Error?) {
defer {
delegatesReferences.removeAll(where: { $0 == self })
}
if let error = error as NSError? {
return promise.reject(error: .capture(.unknown(message: error.description)), cause: error)
}
}
// MARK: Private
private let promise: Promise
}