2021-02-19 08:28:05 -07:00
|
|
|
//
|
|
|
|
// PhotoCaptureDelegate.swift
|
|
|
|
// Cuvent
|
|
|
|
//
|
|
|
|
// Created by Marc Rousavy on 15.12.20.
|
|
|
|
// Copyright © 2020 Facebook. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AVFoundation
|
|
|
|
|
|
|
|
private var delegatesReferences: [NSObject] = []
|
|
|
|
|
2021-03-09 02:53:29 -07:00
|
|
|
// MARK: - PhotoCaptureDelegate
|
|
|
|
|
2021-02-19 08:28:05 -07:00
|
|
|
class PhotoCaptureDelegate: NSObject, AVCapturePhotoCaptureDelegate {
|
2021-03-09 02:53:29 -07:00
|
|
|
// MARK: Lifecycle
|
2021-02-19 08:28:05 -07:00
|
|
|
|
|
|
|
required init(promise: Promise) {
|
|
|
|
self.promise = promise
|
|
|
|
super.init()
|
|
|
|
delegatesReferences.append(self)
|
|
|
|
}
|
|
|
|
|
2021-03-09 02:53:29 -07:00
|
|
|
// MARK: Internal
|
|
|
|
|
2021-02-19 08:28:05 -07:00
|
|
|
func photoOutput(_: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
|
|
|
|
defer {
|
|
|
|
delegatesReferences.removeAll(where: { $0 == self })
|
|
|
|
}
|
2021-03-26 09:20:57 -06:00
|
|
|
if let error = error as NSError? {
|
|
|
|
return promise.reject(error: .capture(.unknown(message: error.description)), cause: error)
|
2021-02-19 08:28:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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 })
|
|
|
|
}
|
2021-03-26 09:20:57 -06:00
|
|
|
if let error = error as NSError? {
|
|
|
|
return promise.reject(error: .capture(.unknown(message: error.description)), cause: error)
|
2021-02-19 08:28:05 -07:00
|
|
|
}
|
|
|
|
}
|
2021-03-09 02:53:29 -07:00
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private let promise: Promise
|
2021-02-19 08:28:05 -07:00
|
|
|
}
|