2016-10-06 9 views
1

プレビューカメラの出力を変更するのに複数のビューとズームボタンが必要なので、セッションの初期化にはシングルトンを正しく使用すると思いますが、良い情報を見つけることができない、誰かが私を助けることができる?シングルトンを持つAVCaptureSession。管理方法

+0

AVCaptureSessionのためのシングルトンを持つクラスを作成してください。このhttp://krakendev.io/blog/the-right-way-to-write-a-singletonを使ってシングルトンを作成することもできます。 –

答えて

0

UPDATE

大丈夫なので、私はそれを書くために何とか管理し、それは大丈夫だ場合、私は知らないが、ここでのコードは次のとおりです。

protocol Singleton: class { 
static var sharedInstance: Self { get } 
} 


final class AVFSessionSingleton: Singleton { 
static let sharedInstance = AVFSessionSingleton() 
private init() { 
    session = newVideoCaptureSession()! 
} 


var session: AVCaptureSession! 
var imageOutput : AVCaptureStillImageOutput? 

//FUNCTION 

func newVideoCaptureSession() -> AVCaptureSession? { 

    func initCaptureDevice() -> AVCaptureDevice? { 
     var captureDevice: AVCaptureDevice? 
     let devices: NSArray = AVCaptureDevice.devices() as NSArray 

     for device: Any in devices { 
      if (device as AnyObject).position == AVCaptureDevicePosition.back { 
       captureDevice = device as? AVCaptureDevice 
      } 
     } 
     print("device inited") 
     return captureDevice 
    } 

    func initOutput() { 
     self.imageOutput = AVCaptureStillImageOutput() 
    } 

    func initInputDevice(captureDevice : AVCaptureDevice) -> AVCaptureInput? { 
     var deviceInput : AVCaptureInput? 
     do { 
      deviceInput = try AVCaptureDeviceInput(device: captureDevice) 
     } 
     catch _ { 
      deviceInput = nil 
     } 
     return deviceInput 
    } 

    func initSession(deviceInput: AVCaptureInput) { 
     self.session = AVCaptureSession() 
     self.session?.sessionPreset = AVCaptureSessionPresetPhoto 
     self.session?.addInput(deviceInput) 
     self.session?.addOutput(self.imageOutput!) 
    } 

    return session 
} 

} 

だから今、私はそれを呼びたいですその方法でプレビューのレイアウトを管理することができます。どんな提案もお願いします....

関連する問題