我试图使用状态栏方向纠正它,但似乎没有任何影响.我不需要将这些图像保存到相机胶卷,它现在就在那里验证.我不确定这对方向是否有任何影响.
import UIKitimport AVFoundationclass VIEwController: UIVIEwController {let captureSession = AVCaptureSession()let stillimageOutput = AVCaptureStillimageOutput()var avCaptureVIDeoPrevIEw : AVCaptureVIDeoPrevIEwLayer?var error: NSError?overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() let devices = AVCaptureDevice.devices().filter{解决方法 编辑,这不适用于使用前置摄像头拍摄的照片,它们在原生照片应用程序中显示正常,但在Mac上的照片中共享或从parse.com加载时不能显示.import UIKitimport AVFoundationclass VIEwController: UIVIEwController {let captureSession = AVCaptureSession()let stillimageOutput = AVCaptureStillimageOutput()var avCaptureVIDeoPrevIEw : AVCaptureVIDeoPrevIEwLayer?var error: NSError?overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() let devices = AVCaptureDevice.devices().filter{ .hasMediaType(AVMediaTypeVIDeo) } if let captureDevice = devices.first as? AVCaptureDevice { captureSession.addinput(AVCaptureDeviceinput(device: captureDevice,action:"savetoCamera:")) vIEw.addSubvIEw(cameraPrevIEw) } } // Add a selfIE button if device is capable for device in devices { if device.position == AVCaptureDeviceposition.Front { // Add selfIE button println("selfIE capable") } }}overrIDe func vIEwWillLayoutSubvIEws() { if avCaptureVIDeoPrevIEw != nil { // OrIEntation if (avCaptureVIDeoPrevIEw!.connection.supportsVIDeoOrIEntation == true) { var newOrIEntation : AVCaptureVIDeoOrIEntation? switch UIDevice.currentDevice().orIEntation { case UIDeviceOrIEntation.Portrait: newOrIEntation = AVCaptureVIDeoOrIEntation.Portrait case UIDeviceOrIEntation.PortraitUpsIDeDown: newOrIEntation = AVCaptureVIDeoOrIEntation.PortraitUpsIDeDown case UIDeviceOrIEntation.Landscapeleft: newOrIEntation = AVCaptureVIDeoOrIEntation.LandscapeRight; case UIDeviceOrIEntation.LandscapeRight: newOrIEntation = AVCaptureVIDeoOrIEntation.Landscapeleft default: newOrIEntation = AVCaptureVIDeoOrIEntation.Portrait } avCaptureVIDeoPrevIEw!.connection.vIDeoOrIEntation = newOrIEntation! } // Frame avCaptureVIDeoPrevIEw!.bounds = CGRectMake(0.0,vIEw.bounds.mIDY) avCaptureVIDeoPrevIEw!.vIDeoGravity = AVLayerVIDeoGravityResizeAspectFill }}func savetoCamera(sender: UITapGestureRecognizer) { if let vIDeoConnection = stillimageOutput.connectionWithMediaType(AVMediaTypeVIDeo) { stillimageOutput.captureStillimageAsynchronouslyFromConnection(vIDeoConnection) { (imageDataSampleBuffer,error) -> VoID in var image = UIImage(data: AVCaptureStillimageOutput.jpegStillimageNSDataRepresentation(imageDataSampleBuffer)) var imageOrIEntation : UIImageOrIEntation? switch UIDevice.currentDevice().orIEntation { case UIDeviceOrIEntation.PortraitUpsIDeDown: imageOrIEntation = UIImageOrIEntation.left case UIDeviceOrIEntation.LandscapeRight: imageOrIEntation = UIImageOrIEntation.Down case UIDeviceOrIEntation.Landscapeleft: imageOrIEntation = UIImageOrIEntation.Up case UIDeviceOrIEntation.Portrait: imageOrIEntation = UIImageOrIEntation.Right default: imageOrIEntation = UIImageOrIEntation.Right } var newImage = UIImage(CGImage: image!.CGImage,orIEntation: imageOrIEntation!) UIImageWritetoSavedPhotosAlbum(newImage!,nil) } }}overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning()}}.hasMediaType(AVMediaTypeVIDeo) } if let captureDevice = devices.first as? AVCaptureDevice { captureSession.addinput(AVCaptureDeviceinput(device: captureDevice,error: &error)) captureSession.sessionPreset = AVCaptureSessionPresetMedium captureSession.startRunning() stillimageOutput.outputSettings = [AVVIDeoCodecKey:AVVIDeoCodecJPEG] if captureSession.canAddOutput(stillimageOutput) { captureSession.addOutput(stillimageOutput) } avCaptureVIDeoPrevIEw = AVCaptureVIDeoPrevIEwLayer(session: captureSession) if let prevIEwLayer = avCaptureVIDeoPrevIEw { prevIEwLayer.bounds = CGRectMake(0.0,0.0,vIEw.bounds.size.wIDth,vIEw.bounds.size.height) prevIEwLayer.position = CGPointMake(vIEw.bounds.mIDX,vIEw.bounds.mIDY) prevIEwLayer.vIDeoGravity = AVLayerVIDeoGravityResizeAspectFill let cameraPrevIEw = UIVIEw(frame: CGRectMake(0.0,vIEw.bounds.size.height)) cameraPrevIEw.layer.addSublayer(prevIEwLayer) cameraPrevIEw.addGestureRecognizer(UITapGestureRecognizer(target: self,action:"savetoCamera:")) vIEw.addSubvIEw(cameraPrevIEw) } } // Add a selfIE button if device is capable for device in devices { if device.position == AVCaptureDeviceposition.Front { // Add selfIE button println("selfIE capable") } }}overrIDe func vIEwWillLayoutSubvIEws() { if avCaptureVIDeoPrevIEw != nil { // OrIEntation if (avCaptureVIDeoPrevIEw!.connection.supportsVIDeoOrIEntation == true) { avCaptureVIDeoPrevIEw!.connection.vIDeoOrIEntation = interfaceOrIEntationToVIDeoOrIEntation(UIApplication.sharedApplication().statusbarOrIEntation) } // Frame avCaptureVIDeoPrevIEw!.bounds = CGRectMake(0.0,vIEw.bounds.size.height) avCaptureVIDeoPrevIEw!.position = CGPointMake(vIEw.bounds.mIDX,vIEw.bounds.mIDY) avCaptureVIDeoPrevIEw!.vIDeoGravity = AVLayerVIDeoGravityResizeAspectFill }}func interfaceOrIEntationToVIDeoOrIEntation(orIEntation: UIInterfaceOrIEntation) -> AVCaptureVIDeoOrIEntation { switch orIEntation { case UIInterfaceOrIEntation.Portrait: return AVCaptureVIDeoOrIEntation.Portrait case UIInterfaceOrIEntation.PortraitUpsIDeDown: return AVCaptureVIDeoOrIEntation.PortraitUpsIDeDown case UIInterfaceOrIEntation.Landscapeleft: return AVCaptureVIDeoOrIEntation.Landscapeleft case UIInterfaceOrIEntation.LandscapeRight: return AVCaptureVIDeoOrIEntation.LandscapeRight default: return AVCaptureVIDeoOrIEntation.Portrait }}func savetoCamera(sender: UITapGestureRecognizer) { if let vIDeoConnection = stillimageOutput.connectionWithMediaType(AVMediaTypeVIDeo) { stillimageOutput.captureStillimageAsynchronouslyFromConnection(vIDeoConnection) { (imageDataSampleBuffer,error) -> VoID in var image = UIImage(data: AVCaptureStillimageOutput.jpegStillimageNSDataRepresentation(imageDataSampleBuffer)) // this is not working var orIEntation : UIImageOrIEntation? switch UIApplication.sharedApplication().statusbarOrIEntation.rawValue { case 1: orIEntation = UIImageOrIEntation.Up case 3: orIEntation = UIImageOrIEntation.left case 4: orIEntation = UIImageOrIEntation.Right default: orIEntation = UIImageOrIEntation.Up } let newImage = UIImage(CGImage: image?.CGImage,scale: 1.0,orIEntation: orIEntation!) UIImageWritetoSavedPhotosAlbum(newImage,nil,nil) } }}overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning()}}
感谢@XJones在这里回答Why does AVCaptureVideoOrientation landscape modes result in upside down still images?
总结以上是内存溢出为你收集整理的ios – AVMediaTypeVideo捕获静止图像方向全部内容,希望文章能够帮你解决ios – AVMediaTypeVideo捕获静止图像方向所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)