2015-10-12 63 views
1

我到處尋找,我無法弄清楚如何以這種方式切換閃光燈,當我拍照時,它會閃爍4次,就像它在iPhone相機上一樣。如何使用閃光燈在自定義相機上拍照?

我使用這個功能可切換閃光燈...

func toggleFlash() { 
    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 
    if (device.hasTorch) { 
     device.lockForConfiguration(nil) 
     if (device.torchMode == AVCaptureTorchMode.On) { 
      device.torchMode = AVCaptureTorchMode.Off 
     } else { 
      device.setTorchModeOnWithLevel(1.0, error: nil) 
     } 
     device.unlockForConfiguration() 
    } 
} 

而且我用這個功能來拍照......

func didPressTakePhoto(){ 
    if let videoConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo){ 
     videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait 
     stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { 
      (sampleBuffer, error) in 

      if sampleBuffer != nil { 

       var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) 
       var dataProvider = CGDataProviderCreateWithCFData(imageData) 
       var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault) 

       var image:UIImage! 

       if self.camera == true { 
        image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right) 

       } else { 
        image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.LeftMirrored) 

       } 
       self.tempImageView.image = image 
       self.tempImageView.hidden = false 
      } 


     }) 
    } 


} 
+0

我認爲這已經在這裏得到解答:http://stackoverflow.com/a/32301753/4503700 –

+0

@CharlesTruluck不,我試圖讓我可以在拍照時加入閃光燈。 –

+0

對不起!我以爲你的意思是沒有工作 –

回答

1

這裏是一個非常混亂的方式。不乾淨。

func didPressTakePhoto(){ 

toggleFlash() 
sleep(1) 
toggleFlash() 

if let videoConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo){ 
    videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait 
    stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { 
     (sampleBuffer, error) in 

     if sampleBuffer != nil { 

      toggleFlash() 

      var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) 
      var dataProvider = CGDataProviderCreateWithCFData(imageData) 
      var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault) 

      var image:UIImage! 
      if self.camera == true { 
       image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right) 

      } else { 
       image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.LeftMirrored) 

      } 
      self.tempImageView.image = image 
      self.tempImageView.hidden = false 
     } 


    }) 
} 


} 

這是非常混亂,只是一個想法。 你也可以使用NSTimer將它啓動兩次。

+0

感謝你!但是時機稍微偏離了一點。我一直在睡覺,我一直沒有弄清楚。我應該怎麼設置呢? –

+0

當我<= 2'到'while while <= 4'時,你可以嘗試改變'。告訴我看起來如何。 –

+0

我會去嘗試一下!此外,我似乎無法將睡眠設置爲雙倍。 –

相關問題