2015-12-13 8 views
12

近接センサーが有効になっている場合、リアビューカメラを使用してデバイスに画像を取得するのに問題があります。カメラのプレビューが表示されないようにするには、デバイスが写真を撮ってimageViewに表示させたいだけです。私は近接センサー作業を持っている、と私は近接センサが有効になっているときに画像を撮影するためにimagePicker.takePicture()を使用していますが、それが動作するようには思えません。プログラムでユーザー入力なしで画像を取得するために使用できるメソッド/関数は何ですか?近接センサーを使用して写真を撮る方法は?

これは、これまでの私のコードです:

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate { 

@IBOutlet var imageView: UIImageView! 

var imagePicker: UIImagePickerController! 

//*The function in question* 
func proximityChanged(notification: NSNotification) { 
    let device = notification.object as? UIDevice 
    if device?.proximityState == true { 
     print("\(device) detected!") 
+0

あなたは明確でした:動作しないどの部分?近接センサはトリガされていますが、画像が表示されていないか、またはトリガされていませんか? – TwoStraws

+0

はい近接センサーがトリガーされていますが、画像が撮影されていません。 –

+0

AVFoundationを使用し、画像ピッカーをまったく通過しないほうがよいでしょうか? – matt

答えて

2

あなたはUIImagePickerControllerで写真を取り込む悩みを持っている場合、私はAVFoundationを使用してお勧めします。以下は

は実施例です。フォトキャプチャは、近接センサによってトリガされます。

あなたがそれを必要とする場合は、プレビューを追加することができます。

import UIKit 
import AVFoundation 

final class CaptureViewController: UIViewController { 

    @IBOutlet weak var imageView: UIImageView! 

    private static let captureSessionPreset = AVCaptureSessionPresetPhoto 
    private var captureSession: AVCaptureSession! 
    private var photoOutput: AVCaptureStillImageOutput! 
    private var initialized = false 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     initialized = setupCaptureSession() 
    } 

    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 
     if initialized { 
      captureSession.startRunning() 
      UIDevice.currentDevice().proximityMonitoringEnabled = true 
      NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(proximityStateDidChange), name: UIDeviceProximityStateDidChangeNotification, object: nil) 
     } 
    } 

    override func viewDidDisappear(animated: Bool) { 
     super.viewDidDisappear(animated) 
     if initialized { 
      NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceProximityStateDidChangeNotification, object: nil) 
      UIDevice.currentDevice().proximityMonitoringEnabled = false 
      captureSession.stopRunning() 
     } 
    } 

    dynamic func proximityStateDidChange(notification: NSNotification) { 
     if UIDevice.currentDevice().proximityState { 
      captureImage() 
     } 
    } 

    // MARK: - Capture Image 

    private func captureImage() { 
     if let c = findConnection() { 
      photoOutput.captureStillImageAsynchronouslyFromConnection(c) { sampleBuffer, error in 
       if let jpeg = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer), 
        let image = UIImage(data: jpeg) 
       { 
        dispatch_async(dispatch_get_main_queue()) { [weak self] in 
         self?.imageView.image = image 
        } 
       } 
      } 
     } 
    } 

    private func findConnection() -> AVCaptureConnection? { 
     for c in photoOutput.connections { 
      let c = c as? AVCaptureConnection 
      for p in c?.inputPorts ?? [] { 
       if p.mediaType == AVMediaTypeVideo { 
        return c 
       } 
      } 
     } 
     return nil 
    } 

    // MARK: - Setup Capture Session 

    private func setupCaptureSession() -> Bool { 
     captureSession = AVCaptureSession() 
     if captureSession.canSetSessionPreset(CaptureViewController.captureSessionPreset) { 
      captureSession.sessionPreset = CaptureViewController.captureSessionPreset 
      if setupCaptureSessionInput() && setupCaptureSessionOutput() { 
       return true 
      } 
     } 
     return false 
    } 

    private func setupCaptureSessionInput() -> Bool { 
     if let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo), 
      let captureDeviceInput = try? AVCaptureDeviceInput.init(device: captureDevice) 
     { 
      if captureSession.canAddInput(captureDeviceInput) { 
       captureSession.addInput(captureDeviceInput) 
       return true 
      } 
     } 
     return false 
    } 

    private func setupCaptureSessionOutput() -> Bool { 
     photoOutput = AVCaptureStillImageOutput() 
     photoOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG] 
     if captureSession.canAddOutput(photoOutput) { 
      captureSession.addOutput(photoOutput) 
      return true 
     } 
     return false 
    } 

} 
関連する問題