首页 文章

相机仅以纵向模式快速拍照

提问于
浏览
3

如何将相机设置为横向模式?每次拍照时,图像都会保存为肖像图像 . 当设备处于横向模式时,照片看起来很好但如果我在相机胶卷中看到它仍然是纵向模式 . 这是我的拍照功能:

// take a photo
@IBAction func takePhoto(sender: AnyObject) {
self.fullScreenView.hidden = false
self.recordButton.enabled = false
self.takephoto.enabled = false
self.recordButton.hidden = true
self.takephoto.hidden = true

session.startRunning()

// customize the quality level or bitrate of the output photo
session.sessionPreset = AVCaptureSessionPresetPhoto

// add the AVCaptureVideoPreviewLayer to the view and set the view in fullscreen
fullScreenView.frame = view.bounds
videoPreviewLayer.frame = fullScreenView.bounds
fullScreenView.layer.addSublayer(videoPreviewLayer)

// add action to fullScreenView
gestureFullScreenView = UITapGestureRecognizer(target: self, action: #selector(ViewController.takePhoto(_:)))
self.fullScreenView.addGestureRecognizer(gestureFullScreenView)

// add action to myView
gestureView = UITapGestureRecognizer(target: self, action: #selector(ViewController.setFrontpage(_:)))
self.view.addGestureRecognizer(gestureView)

if (preview == true) {
    if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
        // code for photo capture goes here...

        stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
            // process the image data (sampleBuffer) here to get an image file we can put in our view

            if (sampleBuffer != nil) {
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let image = UIImage(data: imageData, scale: 1.0)

                self.fullScreenView.hidden = true
                self.fullScreenView.gestureRecognizers?.forEach(self.fullScreenView.removeGestureRecognizer)
                self.session.stopRunning()

                // save image to the library
                UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)

                self.imageViewBackground = UIImageView(frame: self.view.bounds)
                self.imageViewBackground.image = image
                self.imageViewBackground.tag = self.key

                self.view.addSubview(self.imageViewBackground)
            }
        })
    }
}
else {
    preview = true
}
}

我的预览看起来像那样,那没关系:

http://img5.fotos-hochladen.net/uploads/bildschirmfotom4s7diaehy.png

但最终它看起来像这样:

http://img5.fotos-hochladen.net/uploads/bildschirmfoto3c2rlwtevf.png

提前致谢!

1 回答

  • 0

    可能的解决方案:将图像保存为JPEG而不是PNG

    这是因为PNG不存储方向信息 . 将照片另存为JPG,它将正确定位 .

    使用此代码在拍摄图像后立即将图像转换为JPG(第二行是此处的操作一行):

    let image = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
    let imageData:NSData = UIImageJPEGRepresentation(image, 0.9)! // 0.9 is compression value: 0.0 is most compressed/lowest quality and 1.0 is least compressed/highest quality
    

    来源更多信息:https://stackoverflow.com/a/34796890/5700898


    如果这不起作用

    I've edited your code in a couple places, see if the below code now works as you hope:

    // initialize saving photo capabilities
    func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
            if error == nil {
                print("image saved")
            } else {
                print("save error: \(error?.localizedDescription)")
            }
    }
    
    // take a photo
    @IBAction func takePhoto(sender: AnyObject) {
    self.fullScreenView.hidden = false
    self.recordButton.enabled = false
    self.takephoto.enabled = false
    self.recordButton.hidden = true
    self.takephoto.hidden = true
    
    session.startRunning()
    
    // customize the quality level or bitrate of the output photo
    session.sessionPreset = AVCaptureSessionPresetPhoto
    
    // add the AVCaptureVideoPreviewLayer to the view and set the view in fullscreen
    fullScreenView.frame = view.bounds
    videoPreviewLayer.frame = fullScreenView.bounds
    fullScreenView.layer.addSublayer(videoPreviewLayer)
    
    // add action to fullScreenView
    gestureFullScreenView = UITapGestureRecognizer(target: self, action: #selector(ViewController.takePhoto(_:)))
    self.fullScreenView.addGestureRecognizer(gestureFullScreenView)
    
    // add action to myView
    gestureView = UITapGestureRecognizer(target: self, action: #selector(ViewController.setFrontpage(_:)))
    self.view.addGestureRecognizer(gestureView)
    
    if (preview == true) {
        if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
            // code for photo capture goes here...
    
            stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
                // process the image data (sampleBuffer) here to get an image file we can put in our view
    
                if (sampleBuffer != nil) {
                    self.stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
            if let videoConnection = self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo){
                videoConnection.videoOrientation = self.interfaceToVideoOrientation()
                self.stillImageOutput!.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
                    (sampleBuffer, error) in
                    let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                    let image = UIImage(data: imageData)
    
                    self.fullScreenView.hidden = true
                    self.fullScreenView.gestureRecognizers?.forEach(self.fullScreenView.removeGestureRecognizer)
                    self.session.stopRunning()
    
                    // save image to the library
                    UIImageWriteToSavedPhotosAlbum(image, self, #selector(ViewController.image(_:didFinishSavingWithError:contextInfo:)), nil)
    
                    self.imageViewBackground = UIImageView(frame: self.view.bounds)
                    self.imageViewBackground.image = image
                    self.imageViewBackground.tag = self.key
    
                    self.view.addSubview(self.imageViewBackground)
                }
            })
        }
    }
    else {
        preview = true
    }
    }
    

相关问题