iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 有没有办法在swift中将相机设置为横向模式?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何将相机设置为横向模式?每次拍照时,图像都会保存为肖像图像.当设备处于横向模式时,照片看起来很好但是如果我在相机胶卷中看到它仍然是纵向模式.
这是我的拍照功能

// 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)

                    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

解决方法

我使用这个函数,这可能对你有用:

override func viewWillTransitionToSize(size: CGSize,withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransitionToSize(size,withTransitionCoordinator: coordinator)

        let orientation = UIDevice.currentDevice().orientation

        if UIDeviceOrientationIsPorTrait(orientation) || UIDeviceOrientationIsLandscape(orientation) {
            if let videoOrientation = AVCaptureVideoOrientation(rawValue: orientation.rawvalue) {
                (captureView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation = videoOrientation
            }
        }
    }

大佬总结

以上是大佬教程为你收集整理的ios – 有没有办法在swift中将相机设置为横向模式?全部内容,希望文章能够帮你解决ios – 有没有办法在swift中将相机设置为横向模式?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。