diff --git a/Demo/ImagePickerDemo/Podfile b/Demo/ImagePickerDemo/Podfile index 0f256f62..78ba7dfe 100644 --- a/Demo/ImagePickerDemo/Podfile +++ b/Demo/ImagePickerDemo/Podfile @@ -1,4 +1,4 @@ -platform :ios, '9.2' +platform :ios, '13.0' use_frameworks! inhibit_all_warnings! diff --git a/ImagePicker.podspec b/ImagePicker.podspec index 92dd7642..2639bc3c 100644 --- a/ImagePicker.podspec +++ b/ImagePicker.podspec @@ -7,7 +7,7 @@ Pod::Spec.new do |s| s.author = { "Hyper Interaktiv AS" => "ios@hyper.no" } s.source = { :git => "https://github.com/hyperoslo/ImagePicker.git", :tag => s.version.to_s } s.social_media_url = 'https://twitter.com/hyperoslo' - s.platform = :ios, '9.0' + s.platform = :ios, '13.0' s.requires_arc = true s.source_files = 'Source/**/*' s.resource_bundles = { 'ImagePicker' => ['Images/*.{png}'] } diff --git a/Package.swift b/Package.swift index d93a6bb7..cf173358 100644 --- a/Package.swift +++ b/Package.swift @@ -5,7 +5,7 @@ import PackageDescription let package = Package( name: "ImagePicker", platforms: [ - .iOS(.v9) + .iOS(.v13) ], products: [ // Products define the executables and libraries produced by a package, and make them visible to other packages. diff --git a/Source/CameraView/CameraMan.swift b/Source/CameraView/CameraMan.swift index 8714823f..d99b822f 100644 --- a/Source/CameraView/CameraMan.swift +++ b/Source/CameraView/CameraMan.swift @@ -33,27 +33,48 @@ class CameraMan { func setupDevices() { // Input AVCaptureDevice - .devices() - .filter { - return $0.hasMediaType(AVMediaType.video) - }.forEach { + .DiscoverySession(deviceTypes: [.builtInTripleCamera,.builtInTelephotoCamera], mediaType: AVMediaType.video, position: .front) + .devices + .forEach { switch $0.position { case .front: self.frontCamera = try? AVCaptureDeviceInput(device: $0) - case .back: - self.backCamera = try? AVCaptureDeviceInput(device: $0) + default: break } } + let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualCamera, .builtInWideAngleCamera], mediaType: .video, position: .back) + var selectedDevice: AVCaptureDevice? + + // Attempt to find a triple camera first + if let tripleCameraDevice = discoverySession.devices.first(where: { $0.deviceType == .builtInTripleCamera }) { + selectedDevice = tripleCameraDevice + } else if let dualCameraDevice = discoverySession.devices.first(where: { $0.deviceType == .builtInDualCamera }) { + // Fallback to dual camera if no triple camera is found + selectedDevice = dualCameraDevice + } else if let wideAngleCameraDevice = discoverySession.devices.first(where: { $0.deviceType == .builtInWideAngleCamera }) { + // Fallback to wide angle camera if no dual camera is found + selectedDevice = wideAngleCameraDevice + } + + // If a device was selected, create the AVCaptureDeviceInput + if let selectedDevice = selectedDevice { + do { + let backCameraInput = try AVCaptureDeviceInput(device: selectedDevice) + self.backCamera = backCameraInput + } catch { + print("Error creating AVCaptureDeviceInput for the selected device: \(error)") + } + } + // Output stillImageOutput = AVCaptureStillImageOutput() stillImageOutput?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG] } func addInput(_ input: AVCaptureDeviceInput) { - configurePreset(input) if session.canAddInput(input) { session.addInput(input) @@ -98,6 +119,10 @@ class CameraMan { } fileprivate func start() { + self.session.beginConfiguration() + + session.sessionPreset = .photo + // Devices setupDevices() @@ -109,6 +134,8 @@ class CameraMan { session.addOutput(output) } + self.session.commitConfiguration() + queue.async { self.session.startRunning() @@ -203,15 +230,19 @@ class CameraMan { } } - func zoom(_ zoomFactor: CGFloat) { - guard let device = currentInput?.device, device.position == .back else { return } + func zoom(_ zoomFactor: CGFloat) { + guard var device = currentInput?.device, device.position == .back else { return } - queue.async { - self.lock { - device.videoZoomFactor = zoomFactor - } + queue.async { + do + { + var newZoomFactor = zoomFactor < 1.0 ? 1.0 : zoomFactor + self.lock { + device.ramp(toVideoZoomFactor: newZoomFactor, withRate: 4.0) + } + } + } } - } // MARK: - Lock @@ -228,23 +259,4 @@ class CameraMan { block() session.commitConfiguration() } - - // MARK: - Preset - - func configurePreset(_ input: AVCaptureDeviceInput) { - for asset in preferredPresets() { - if input.device.supportsSessionPreset(AVCaptureSession.Preset(rawValue: asset)) && self.session.canSetSessionPreset(AVCaptureSession.Preset(rawValue: asset)) { - self.session.sessionPreset = AVCaptureSession.Preset(rawValue: asset) - return - } - } - } - - func preferredPresets() -> [String] { - return [ - AVCaptureSession.Preset.high.rawValue, - AVCaptureSession.Preset.high.rawValue, - AVCaptureSession.Preset.low.rawValue - ] - } } diff --git a/Source/CameraView/CameraView.swift b/Source/CameraView/CameraView.swift index c190224f..fb0bf9a5 100644 --- a/Source/CameraView/CameraView.swift +++ b/Source/CameraView/CameraView.swift @@ -96,9 +96,6 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate var locationManager: LocationManager? var startOnFrontCamera: Bool = false - private let minimumZoomFactor: CGFloat = 1.0 - private let maximumZoomFactor: CGFloat = 3.0 - private var currentZoomFactor: CGFloat = 1.0 private var previousZoomFactor: CGFloat = 1.0 @@ -156,7 +153,7 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate layer.backgroundColor = configuration.mainColor.cgColor layer.autoreverses = true - layer.videoGravity = AVLayerVideoGravity.resizeAspectFill + layer.videoGravity = AVLayerVideoGravity.resizeAspect view.layer.insertSublayer(layer, at: 0) layer.frame = view.layer.frame @@ -264,11 +261,9 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate func zoomTo(_ zoomFactor: CGFloat) { guard let device = cameraMan.currentInput?.device else { return } - let maximumDeviceZoomFactor = device.activeFormat.videoMaxZoomFactor let newZoomFactor = previousZoomFactor * zoomFactor - currentZoomFactor = min(maximumZoomFactor, max(minimumZoomFactor, min(newZoomFactor, maximumDeviceZoomFactor))) - cameraMan.zoom(currentZoomFactor) + cameraMan.zoom(newZoomFactor) } // MARK: - Tap