如何使用Swift在人像模式下拍摄图像

时间:2019-04-25 10:30:56

标签: ios swift

我经历了苹果公司的AVCam模型,并在我的相机应用中实现了人像模式。但是问题在于捕获的图像将不会处于人像模式,而是照片将由普通相机拍摄。后来我发现我可以通过进入编辑部分并选择“纵向”选项来激活纵向模式。 我希望图片以纵向模式保存。我该怎么办?

1 个答案:

答案 0 :(得分:0)

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(MainCameraVC.orientationChanged(notification:)), name: Notification.Name.UIDeviceOrientationDidChange, object: nil)
    }

    func orientationChanged(notification: Notification) {
        let deviceOrientation = UIDevice.current.orientation
        var angle: Double?

        switch deviceOrientation {
        case .portrait:
            angle = 0
            break
        case .portraitUpsideDown:
            angle = Double.pi
            break
        case .landscapeLeft:
            angle = Double.pi / 2
            break
        case .landscapeRight:
            angle = -Double.pi / 2
            break
        default:
            break
        }

        if let angle = angle {
            let transform = CGAffineTransform(rotationAngle: CGFloat(angle))

            UIView.animate(withDuration: 0.3, animations: {
                self.label.transform = transform
                self.button.transform = transform
                ...
            })
        }
    }
}