首页 > 解决方案 > Swift 4 相机视图出现在 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)
}

标签: swiftipad

解决方案


   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)!)
   }

推荐阅读