2017-03-02 1 views
0

저는 Instagram과 유사한 앱을 만들고 있습니다. ATM에 사진을 캡처하고 장치의 사진 라이브러리에 저장하는 작업 코드가 있습니다. 카메라 미리보기는 후면/전면 카메라 스위치로 작동합니다. 지금 문제는 캡처 기능에 플래시를 .auto 또는 .off로 설정하는 코드 조각을 제공하는 것입니다. 버튼을 누르면 플래시가 작동하여 플래시가 작동합니다. 여기 내 캡처 코드 :AVFoundation - 이미지가 캡처되기 전에 자동/꺼짐으로 플래시를 설정하는 방법

let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 
    if device.hasTorch { 
     do { 
      try device.lockForConfiguration() 
      device.torchMode = AVCaptureTorchMode.On 
      device.torchMode = AVCaptureTorchMode.Off 
      device.torchMode = AVCaptureTorchMode.Auto 
      device.unlockForConfiguration() 
     } catch { 
      print(error) 
     } 
    } 

가 도움이 희망 : 여기 내가 사용하는 내 코드

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { 

     if let error = error { 
      print("error occure : \(error.localizedDescription)") 
     } 

     if let sampleBuffer = photoSampleBuffer, 
      let previewBuffer = previewPhotoSampleBuffer, 
      let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) { 
      print(UIImage(data: dataImage)?.size as Any) 

      let dataProvider = CGDataProvider(data: dataImage as CFData) 
      let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent) 
      let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right) 
      UIImageWriteToSavedPhotosAlbum(image, self, nil, nil) 
      self.cameracapture.image = image 
     } else { 
      print("some error here") 
     } 
    } 

답변

0

입니다. :)

+0

이 코드를 해봤를, 그것은 작동하지만, 첫 번째 줄은 사용되지 않는 방법을 가지고, 아이폰 OS (10)에 대한 새로운 하나입니다 : AVCaptureDevice.defaultDevice (withMediaType : AVMediaTypeVideo) –

+0

코드의 나머지 부분에 대해서는 장치가 선택 사항이며 xcode 8.2는 나에게? 장치 후. –

+0

예. 내 코드는 Swift 2.3을 사용합니다. :) –

0
func toggleFlash() { 
    let avDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) 
    if (avDevice?.hasTorch)! { 
     do { 
      _ = try avDevice?.lockForConfiguration() 
     } catch let err as NSError { 
      report(err.localizedDescription) 
     } 

     if (avDevice?.isTorchActive)! { 
      avDevice?.torchMode = AVCaptureTorchMode.off 
     } else { 
      do { 
       _ = try avDevice?.setTorchModeOnWithLevel(1.0) 
      } catch let err as NSError { 
       report(err.localizedDescription) 
      } 
     } 
     avDevice?.unlockForConfiguration() 
    } 
} 

이것은 예입니다. 또한 자동 모드를 설정할 수 있습니다

avDevice?.torchMode = AVCaptureTorchMode.auto 
+0

안녕하세요, 도움을 주셔서 감사합니다. 구현하고 작동하지만 코드가 내 카메라보기를 중단합니다. 이미지가 멈추지 만 앱이 실행되지 않습니다. 카메라의 버튼을 누르기 전까지 충돌이 일어나면이 문제를 해결할 수 있습니까? –

+0

업데이트 : 충돌이 해결되었습니다 (앞 카메라 모드에서 문제가없는 경우). 이제 버튼을 눌러 사진을 촬영하면 앱이 저장하지만 플래시가 어두운 환경에서 자동으로 설정되면 작동하지 않습니다. –