swift - 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)
}
解决方案
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)!)
}
推荐阅读
- c++ - Boost::beast:多个 async_write 调用正在触发一个断言错误
- ember.js - 如何在 Ember 中导入 ZeroEx npm 模块
- stack - postscript 堆栈溢出在 mac 和 linux 上不同,并受字体设置的影响
- javascript - 如果值存在于所有数组中,则返回 true
- go - os.Read() 是怎么工作的?戈朗
- php - 使用 Laravel 和 Mongodb 进行多 Jwt 身份验证
- ajaxcontroltoolkit - 使用 ajaxToolkit:AjaxFileUpload 上传 MOV 文件时 AjaxFileUpload.axd 失败
- javascript - 来自多方的数据
- python - 井字游戏嵌套列表问题
- ios - 当应用程序未处于运行状态(非活动状态)时,下载所有推送通知