面具效果是旋转照片

时间:2017-05-04 13:44:33

标签: ios swift swift3 rotation mask

我的代码拍了一张照片。然后保存它,并在其上应用蒙版效果。唯一的问题是它从我拍摄肖像照片的方式保存在-90。我想做的就是拍摄肖像照片,并保存照片,不要应用旋转。

[应用蒙版时保存的图片] [2]

[面具] [3]

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let red = info[UIImagePickerControllerOriginalImage] as! UIImage
             self.dismiss(animated: true)
//Sugguested Code
    red.imageOrientation = UIDevice.current.orientation.imageOrientation
//


    self.currentImageView?.image = red

    let image = photo.image
       let maskingImage = UIImage(named: "mask")
    photo.image = maskImage(image: image!, mask: maskingImage!)
    photo.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
    photo.contentMode = .scaleAspectFit
    photo.clipsToBounds = true

  }

1 个答案:

答案 0 :(得分:0)

当您创建UIImage时,您需要确保为它们提供正确的方向。这将从手机获取方向并将其应用于图像

extension UIDeviceOrientation {
    public var imageOrientation: UIImageOrientation? {
        switch self {
        case .portrait: return .right
        case .portraitUpsideDown: return .left
        case .landscapeLeft: return .up
        case .landscapeRight: return .down
        default: return nil
        }
    }
}
image.imageOrientation = UIDevice.current.orientation.imageOrientation