在iPad上横向显示Swift 4相机视图

时间:2018-11-19 21:54:25

标签: swift ipad

我正在构建一个可以拍照的iPad应用程序。当我执行配置和捕获会话时,摄像机视图会横向显示。如何在iPad上旋转相机使其不处于-90度?我正在使用Swift 4.1和iOS 12.1。感谢您的指导。

配置

   private func configure() {
    // Preset the session for taking photo in full resolution
    captureSession.sessionPreset = AVCaptureSession.Preset.photo

    // Get the front and back-facing camera for taking photos
    let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified)

    for device in deviceDiscoverySession.devices {
        if device.position == .back {
            backFacingCamera = device
        } else if device.position == .front {
            frontFacingCamera = device
        }
    }

    currentDevice = backFacingCamera

    guard let captureDeviceInput = try? AVCaptureDeviceInput(device: currentDevice) else {
        return
    }

    // Configure the session with the output for capturing still images
    stillImageOutput = AVCapturePhotoOutput()

    // Configure the session with the input and the output devices
    captureSession.addInput(captureDeviceInput)
    captureSession.addOutput(stillImageOutput)

    // Provide a camera preview
    cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    view.layer.addSublayer(cameraPreviewLayer!)
    cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    cameraPreviewLayer?.frame = view.layer.frame

    // Bring the camera button to front
    view.bringSubviewToFront(cameraButton)
    captureSession.startRunning() 
 }

捕获

 @IBAction func capture(sender: UIButton) {
    // Set photo settings
    let photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])
    photoSettings.isAutoStillImageStabilizationEnabled = true
    photoSettings.isHighResolutionPhotoEnabled = true
    //photoSettings.flashMode = .off

    stillImageOutput.isHighResolutionCaptureEnabled = true
    stillImageOutput.capturePhoto(with: photoSettings, delegate: self)
}

1 个答案:

答案 0 :(得分:0)

   func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
     guard let imageBuffer = photo.pixelBuffer else { return }

     let ciimgCapturedPortrait = CIImage(cvPixelBuffer: imageBuffer).oriented(forExifOrientation: 5)
     let uiimageCapturedPortrait = UIImage(cgImage: CIContext(options: nil).createCGImage(ciimg, from: ciimg.extent)!)
   }