Created
August 14, 2020 02:34
-
-
Save DaisukeNagata/91d78147e3df42b342d0b33a2ea91fde to your computer and use it in GitHub Desktop.
SwiftUI_Representable_Camera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import AVFoundation | |
struct ContentView: View { | |
var body: some View { | |
RepresentableViewController() | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct RepresentableViewController: UIViewControllerRepresentable { | |
@State private var viewController = ViewController() | |
func makeUIViewController(context: UIViewControllerRepresentableContext<RepresentableViewController>) -> UIViewController { | |
return viewController | |
} | |
func makeCoordinator() -> Coordintor { return Coordintor(owner: self) } | |
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<RepresentableViewController>) {} | |
final class Coordintor: NSObject, UIDocumentInteractionControllerDelegate { | |
let owner: RepresentableViewController | |
init(owner: RepresentableViewController) { | |
self.owner = owner | |
} | |
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { | |
return owner.viewController | |
} | |
func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) { | |
controller.delegate = nil | |
} | |
} | |
} | |
class ViewController: UIViewController { | |
let aVC = CameraObject() | |
var cameraView = UIImageView() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
cameraView.frame = view.frame | |
view.addSubview(cameraView) | |
cameraView = aVC.inSideOutSideCameraSet(cameraView: cameraView) | |
} | |
} | |
class CameraObject: NSObject { | |
var captureDevice = AVCaptureDevice.default(for: .video) | |
var stillImageOutput: AVCaptureMovieFileOutput? | |
func cameraWithPosition(_ position: AVCaptureDevice.Position) -> AVCaptureDevice? { | |
let deviceDescoverySession = | |
AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], | |
mediaType: AVMediaType.video, | |
position: AVCaptureDevice.Position.unspecified) | |
for device in deviceDescoverySession.devices { | |
if device.position == position { | |
return device | |
} | |
} | |
return nil | |
} | |
func inSideOutSideCameraSet(cameraView: UIImageView ) -> UIImageView { | |
var input: AVCaptureDeviceInput! | |
stillImageOutput = AVCaptureMovieFileOutput() | |
let captureSesion = AVCaptureSession() | |
captureSesion.sessionPreset = AVCaptureSession.Preset.hd1920x1080 | |
if cameraView.frame.width != 0 { | |
captureSesion.beginConfiguration() | |
captureSesion.commitConfiguration() | |
} | |
self.captureDevice = self.cameraWithPosition(.back)! | |
var deviceInput: AVCaptureDeviceInput! | |
do { | |
input = try AVCaptureDeviceInput(device: captureDevice!) | |
deviceInput = try AVCaptureDeviceInput(device: captureDevice!) | |
if captureSesion.canAddInput(deviceInput) { | |
captureSesion.removeInput(input) | |
captureSesion.addInput(deviceInput) | |
if (captureSesion.canAddOutput(stillImageOutput!)) { | |
captureSesion.addOutput(stillImageOutput!) | |
captureSesion.startRunning() | |
var previewLayer: AVCaptureVideoPreviewLayer? | |
previewLayer = AVCaptureVideoPreviewLayer(session: captureSesion) | |
previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill | |
previewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait | |
cameraView.layer.addSublayer(previewLayer!) | |
previewLayer?.frame = cameraView.frame | |
return cameraView | |
} | |
} | |
} catch { | |
print(error) | |
} | |
return cameraView | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
info.plist
add
Privacy - Camera Usage Description
Something message....