1
我做了一個簡單的iOS應用程序,所拍攝照片並將其保存到該設備的圖書館。我期待風景圖像輸出,但什麼是相機生產是一個風景圖像的縱向,說一個50×,圖像旋轉到90度。我希望圖像處於風景中,我不確定我的代碼有什麼問題。斯威夫特3 AVCaptureSession圖像輸出是在縱向而非橫向
我需要我的預覽層是在景觀,所以我把我的AVCaptureVideoOrientation到所需方向。
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.landscapeRight
下面是配置:
override func viewWillAppear(_ animated: Bool) {
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)
for device in devices! {
if (device as AnyObject).position == AVCaptureDevicePosition.back {
do {
let input = try AVCaptureDeviceInput(device: device as! AVCaptureDevice)
if (captureSession.canAddInput(input)) {
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
captureSession.addInput(input)
sessionOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
if (captureSession.canAddOutput(sessionOutput)) {
captureSession.addOutput(sessionOutput)
captureSession.startRunning()
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResize
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.landscapeRight
previewLayer.frame = self.cameraView.bounds
cameraView.layer.addSublayer(previewLayer)
previewLayer.position = CGPoint(x: self.cameraView.frame.width/2, y: self.cameraView.frame.height/2)
previewLayer.bounds = cameraView.frame
cameraView.addSubview(captureBtn)
cameraView.addSubview(shutterTimer)
}
}
} catch let err {
print(err)
return
}
}
}
}
圖像輸出
if let videoConnection = sessionOutput.connection(withMediaType: AVMediaTypeVideo) {
sessionOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (buffer, error) in
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
let image = UIImage(data: imageData!)!
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
print(UIImage(data: imageData!)!)
})
}