2017-03-01 46 views
-2

因此,我正在創建一個照相機應用程序,並且我想要一個用於開/關/自動的閃光燈按鈕,然後當用戶點擊快門黃油時,閃光燈將熄滅啓用。我爲閃光燈創建了一個按鈕,但我不知道該怎麼做。創建一個按鈕以在拍攝照片時切換閃光燈

@IBAction func didTouchFlashButton(sender: UIButton) { 
} 

這是我的按鈕,這是我的按鈕拍照

@IBAction func didPressTakePhoto(_ sender: UIButton) { 

//  UIImagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On 

    if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) { 
     videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait 
     stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in 
      if (sampleBuffer != nil) { 
       let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) 
       let dataProvider = CGDataProvider(data: imageData as! CFData) 
       let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent) 

       let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right) 
       self.photos.append(image) 
       print("Photo saved to array") 
       print(self.photos) 
//     UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) 
      } 

     }) 
    } 
} 

所以基本上我想要的按鈕來告訴是否使用閃光燈或不和這取決於用戶決定它會在didPressTakePhoto

+0

https://developer.apple.com/reference/avfoundation/avcapturedevice/1388116-flashmode – matt

回答

0
//get image picker 
let picker:UIImagePickerController = ... 

picker.cameraFlashMode = sender.isSelected ? .on : .off 
+1

雖然此代碼段可以解決的問題,[inclu發起[解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高你的文章的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – DimaSan