Front facing camera in UIImagePickerController

后端 未结 9 1238
暗喜
暗喜 2020-12-02 06:42

I am developing the front facing camera app in iPad2 by using the UIImagePickerController.

When I capture the image it\'s shows as flipped from left to

9条回答
  •  隐瞒了意图╮
    2020-12-02 07:34

    It looks like AVCaptureDeviceDidStartRunningNotification is no longer available as a means of detecting camera device changes. Also, the cameraDevice property on UIImagePickerController doesn't work with KVO. However, it's still possible to detect camera device changes, as shown below (though long-term support for this solution isn't guaranteed as we're using KVO on a property that isn't explicitly marked as KVO-compliant).

    import AVFoundation
    
    var context = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Register for notifications
        let notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.addObserver(self, selector: #selector(handleCaptureSessionDidStartRunning(_:)), name: AVCaptureSessionDidStartRunningNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(handleCaptureSessionDidStopRunning(_:)), name: AVCaptureSessionDidStopRunningNotification, object: nil)
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    func handleCaptureSessionDidStartRunning(notification: NSNotification) {
        guard let session = notification.object as? AVCaptureSession else { return }
        session.addObserver(self, forKeyPath: "inputs", options: [ .Old, .New ], context: &context)
    }
    
    func handleCaptureSessionDidStopRunning(notification: NSNotification) {
        guard let session = notification.object as? AVCaptureSession else { return }
        session.removeObserver(self, forKeyPath: "inputs")
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer) {
        if context == &self.context {
            if let inputs = change?[NSKeyValueChangeNewKey] as? [AnyObject], captureDevice = (inputs.first as? AVCaptureDeviceInput)?.device {
                switch captureDevice.position {
                case .Back: print("Switched to back camera")
                case .Front: print("Switched to front camera")
                case .Unspecified: break
                }
            }
        } else {
            super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        }
    }
    

    Swift 4+ version:

    import AVFoundation
    
    var context = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Register for notifications
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "AVCaptureSessionDidStartRunningNotification"), object: nil, queue: nil) { (notification) in
                self.handleCaptureSessionDidStartRunning(notification: notification)}
            
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "AVCaptureSessionDidStopRunningNotification"), object: nil, queue: nil) { (notification) in
                self.handleCaptureSessionDidStopRunning(notification: notification)}
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    func handleCaptureSessionDidStartRunning(notification: Notification){
        guard let session = notification.object as? AVCaptureSession else { return }
        session.addObserver(self, forKeyPath: "inputs", options: [ .old, .new ], context: &context)
    }
    
    func handleCaptureSessionDidStopRunning(notification: Notification){
        guard let session = notification.object as? AVCaptureSession else { return }
        session.removeObserver(self, forKeyPath: "inputs")
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if context == &self.context {
            if let inputs = change?[NSKeyValueChangeKey.newKey] as? [AnyObject], let captureDevice = (inputs.first as? AVCaptureDeviceInput)?.device {
                switch captureDevice.position {
                case .back: print("Switched to back camera")
                case .front: print("Switched to front camera")
                case .unspecified: break
                }
            }
        } else {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
    }
    

提交回复
热议问题