저는 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")
}
}
이 코드를 해봤를, 그것은 작동하지만, 첫 번째 줄은 사용되지 않는 방법을 가지고, 아이폰 OS (10)에 대한 새로운 하나입니다 : AVCaptureDevice.defaultDevice (withMediaType : AVMediaTypeVideo) –
코드의 나머지 부분에 대해서는 장치가 선택 사항이며 xcode 8.2는 나에게? 장치 후. –
예. 내 코드는 Swift 2.3을 사용합니다. :) –