세로 모드로 사진을 찍는 맞춤형 카메라가 있습니다.Swift - AVFoundation 캡처 이미지 방향
제 생각에는 가로로 사진을 찍고 저장하려고하면 사진이 세로 모드로 저장됩니다.
은 내가 previewLayer 설정이 기능이 있습니다
static func setupPreviewLayer(view: UIView) {
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
switch UIDevice.current.orientation {
case .portrait:
cameraPreviewLayer?.connection?.videoOrientation =
AVCaptureVideoOrientation.portrait
case .portraitUpsideDown:
cameraPreviewLayer?.connection?.videoOrientation =
AVCaptureVideoOrientation.portraitUpsideDown
case .landscapeLeft:
cameraPreviewLayer?.connection?.videoOrientation =
AVCaptureVideoOrientation.landscapeLeft
case .landscapeRight:
cameraPreviewLayer?.connection?.videoOrientation =
AVCaptureVideoOrientation.landscapeRight
default:
cameraPreviewLayer?.connection?.videoOrientation =
AVCaptureVideoOrientation.portrait
}
cameraPreviewLayer?.frame = view.frame
view.layer.insertSublayer(cameraPreviewLayer!, at: 0)
}
그리고 나는 (내가 옳은 방법이 아니다 생각) viewWillAppear이를 호출합니다. 이 확장
@IBAction func takePhotoBtnPressed(_ sender: Any) {
let settings = AVCapturePhotoSettings()
useFlash(settings: settings)
settings.isAutoStillImageStabilizationEnabled = true
CustomCamera.photoOutput?.capturePhoto(with: settings, delegate: self)
}
및 사용 : 사진을 나보다
extension FishCameraVC: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let imageData = photo.fileDataRepresentation() {
image = UIImage(data: imageData)
performSegue(withIdentifier: "ShowTakenPhoto", sender: nil)
}
}
}
모든 것이 한 내가 세로 모드 사진을 촬영하지만, 같이 잘 작동을 나는 회전 할 때 풍경 사진을 촬영할 수있는 휴대 전화는 사진이 여전히 세로로 촬영됩니다.
맞춤 카메라로 작업하는 것은 처음이며이 문제를 해결하기 위해 어디에서 시작해야할지 모르겠습니다 ... 아무도 내가 정말 고마워 할 수 있다면 도움이 될 것입니다.
감사합니다.
은 --------------- UPDATE 나는이 기능을 추가 한 :
func managePhotoOrientation() -> UIImageOrientation {
var currentDevice: UIDevice
currentDevice = .current
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
var deviceOrientation: UIDeviceOrientation
deviceOrientation = currentDevice.orientation
var imageOrientation: UIImageOrientation!
if deviceOrientation == .portrait {
imageOrientation = .up
print("Device: Portrait")
}else if (deviceOrientation == .landscapeLeft){
imageOrientation = .left
print("Device: LandscapeLeft")
}else if (deviceOrientation == .landscapeRight){
imageOrientation = .right
CustomCamera.cameraPreviewLayer?.connection?.videoOrientation = .landscapeRight
print("Device LandscapeRight")
}else if (deviceOrientation == .portraitUpsideDown){
imageOrientation = .down
print("Device PortraitUpsideDown")
}else{
imageOrientation = .up
}
return imageOrientation
}
를 그리고이 내 확장자를 변경 :
extension FishCameraVC: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let imageData = photo.fileDataRepresentation() {
image = UIImage(data: imageData)
let dataProvider = CGDataProvider(data: imageData as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
self.image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: imageOrientation!)
performSegue(withIdentifier: "ShowTakenPhoto", sender: nil)
}
}
} 이미지는 미리보기에서 올바른 방향으로 표시되지만 이미지를 저장하면 세로 방향으로 표시됩니다 ...
답변 주셔서 감사합니다 .... 나는 사과 조언을 받아서 코드를 완전히 바꿨습니다 ... https://developer.apple.com/library/content/samplecode/AVCam/Listings/Swift_AVCam_CameraViewController_swift.html#//apple_ref/doc/uid/DTS40010112-Swift_AVCam_CameraViewController_swift-DontLinkElementID_15 – Marco
이제 완벽하게 작동합니다 ... – Marco
AVCam은 내 사진 캡처를 기반으로합니다. 그 코드에서 videoOrientation 설정을보아야합니다 – Spads