保存旋转的图片(swift3)
Posted
技术标签:
【中文标题】保存旋转的图片(swift3)【英文标题】:save a rotated picture (swift3) 【发布时间】:2017-04-08 17:14:26 【问题描述】:图片不旋转
图片旋转
我要做的就是拍摄旋转保存的照片。就像使用 avfoundation 构建的自定义相机旋转图片一样。现在我的代码拍摄照片并将它们保存到照片库,就像没有旋转的图片一样。
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCapturePhotoCaptureDelegate
@IBOutlet var cameraDisplay: UIView!
var captureSession : AVCaptureSession!
var cameraOutput : AVCapturePhotoOutput!
var previewLayer : AVCaptureVideoPreviewLayer!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
captureSession = AVCaptureSession()
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
cameraOutput = AVCapturePhotoOutput()
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
if let input = try? AVCaptureDeviceInput(device: device)
if (captureSession.canAddInput(input))
captureSession.addInput(input)
if (captureSession.canAddOutput(cameraOutput))
captureSession.addOutput(cameraOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = CGRect(x: 10, y: 10, width: 700, height: 700)
cameraDisplay.layer.addSublayer(previewLayer)
captureSession.startRunning()
func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer)
if let error = error
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
else
let ac = UIAlertController(title: "Image Saved!", message: "Your image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?)
if let sampleBuffer = photoSampleBuffer,
let previewBuffer = previewPhotoSampleBuffer,
let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer)
let dataProvider = CGDataProvider(data: dataImage as CFData)
let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let imagea = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)
UIImageWriteToSavedPhotosAlbum(imagea, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
@IBAction func TakePhoto(_ sender: Any)
let settings = AVCapturePhotoSettings()
let previewpixel = settings.availablePreviewPhotoPixelFormatTypes.first!
let previewformat = [
kCVPixelBufferPixelFormatTypeKey as String: previewpixel, kCVPixelBufferWidthKey as String: 160,
kCVPixelBufferHeightKey as String : 160]
settings.previewPhotoFormat = previewformat
cameraOutput.capturePhoto(with: settings, delegate: self)
【问题讨论】:
你知道旋转角度吗? 是的,逆时针旋转 90 度。所以我去 - 90 度。 【参考方案1】:更改下一行
UIImage(cgImage: (otherImage?.cgImage!)!, scale: CGFloat(1.0), orientation: .right)
到
UIImage(cgImage: (otherImage?.cgImage!)!, scale: CGFloat(1.0), orientation: .up)
【讨论】:
您知道我如何根据天气情况确定设备是纵向还是横向的方向吗? 您想知道设备是纵向还是横向? 现在它应该是纵向的。但是如果设备处于纵向图像方向是正确的但如果设备处于横向图像方向是左侧,我想做一些事情。 检查此***.com/questions/25796545/…,如果您认为答案正确,请为以上答案投票。【参考方案2】:您也可以调用 如果让连接 = cameraOutput.connection(withMediaType: AVMediaTypeVideo) connection.videoOrientation = 方向 在调用 cameraOutput.capturePhoto() 之前。可变方向类型为枚举 AVCaptureVideoOrientation
【讨论】:
以上是关于保存旋转的图片(swift3)的主要内容,如果未能解决你的问题,请参考以下文章