通过编程方式,我在应用程序中从相机中捕获了图像。它被很好地提取,但当我切换到另一个视图时,我想将图像旋转为横向,并关闭该视图。我从相机中捕获了图像。当我从照片库中提取图像时,没有发现任何问题。
以下图片是我的原始图片。Screen Shot
我想旋转图像Screen Shot
以下代码:
var captureSesssion : AVCaptureSession!
var cameraOutput : AVCapturePhotoOutput!
var previewLayer : AVCaptureVideoPreviewLayer!
var device:AVCaptureDevice!
var currentViewControl = UIViewController()
var tempImageHolder = UIImage()
func beginCamera(_ targetView: UIView, _ currentVC: UIViewController)
{
currentViewControl = currentVC
// Do any additional setup after loading the view, typically from a nib.
captureSesssion = AVCaptureSession()
captureSesssion.sessionPreset = AVCaptureSession.Preset.photo
cameraOutput = AVCapturePhotoOutput()
if #available(iOS 11.1, *) {
let availableDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera, .builtInTelephotoCamera, .builtInTrueDepthCamera], mediaType: AVMediaType.video, position: .back).devices
device = availableDevices.first
} else {
// Fallback on earlier versions
device = AVCaptureDevice.default(for: .video)!
}
if let input = try? AVCaptureDeviceInput(device: device!) {
if (captureSesssion.canAddInput(input)) {
captureSesssion.addInput(input)
if (captureSesssion.canAddOutput(cameraOutput)) {
captureSesssion.addOutput(cameraOutput)
let connection = cameraOutput.connection(with: AVFoundation.AVMediaType.video)
connection?.videoOrientation = .portrait
previewLayer = AVCaptureVideoPreviewLayer(session: captureSesssion)
self.previewLayer.frame = targetView.frame
self.previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
//setOutputOrientation()
targetView.layer.addSublayer(previewLayer)
captureSesssion.startRunning()
}
} else {
print("issue here : captureSesssion.canAddInput")
}
} else {
print("some problem here")
}
previewLayer?.frame.size = targetView.frame.size
}
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
currentViewControl.present(alert, animated: true)
} else {
}}
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
let imageData = photo.fileDataRepresentation()
let imageSize: Int = imageData!.count
print(imageSize)
tempImageHolder = UIImage(data: (imageData! as NSData) as Data)!
//When use this below line works fine when take images from left landscape, but when take images from right landscape image not show properly screen shot below:
tempImageHolder = UIImage(cgImage: tempImageHolder.cgImage!, scale: tempImageHolder.scale, orientation: .up)
}
}
从左横向拍摄图像时:
输入:Screen Shot
输出:Screen Shot
从右侧横向拍摄图像时
输入:Screen Shot
输出:Screen Shot
UIImage(cgImage: tempImageHolder.cgImage!, scale: tempImageHolder.scale, orientation: .up)
当使用此功能时,从左侧横向拍摄图像时,上述线条工作正常,但从右侧横向拍摄图像时,图像无法正确显示屏幕截图
有人能给我解释一下如何旋转图像吗?任何帮助都将不胜感激。
6条答案
按热度按时间blmhpbnm1#
您应该在捕获照片之前设置输出方向。
h4cxqtbf2#
这真的是一个很烦人的问题,我真的不相信AVFoundation没有任何更简单的东西给我们。但在我的应用程序中,我是通过保持当前设备的方向来实现这一点的。当照片输出返回图像时,使用方向旋转图像,以确保所有图片都是向上的。
最后,使用fixedImage来满足您的需要。请注意,我订阅了方向更改的通知中心。
0vvn1miw3#
}
用途:
4xy9mtcn4#
这里的问题是,在将
UIImage
转换为CGImage
时,您将丢弃图像元数据(包括方向)。fzwojiic5#
尝试以下功能来固定方向:
xvw2m8pv6#
从iOS 4.0开始,当相机拍摄照片时,它在保存之前不旋转它,它