使用.leftMirrored方向保存照片问题

时间:2017-05-18 20:20:13

标签: ios swift uiimage avfoundation uiimageorientation

所以我使用此功能保存了两张照片

let leftMirroredImage = UIImage(cgImage: myImage.cgImage!, scale: myImage.scale, orientation: UIImageOrientation.leftMirrored)

UIImageWriteToSavedPhotosAlbum(myImage, nil, nil, nil)
UIImageWriteToSavedPhotosAlbum(leftMirroredImage, nil, nil, nil) . 

但出于某种原因,当我在“照片”应用中查看时,leftMirroredImage会被切断。像这样enter image description here

这是保存的普通图像enter image description here

我还注意到照片会像.upMirrored , .downMirrored, and .rightMirrored一样被切断。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

您可以使用CIFilter“CIAffineTransform”并将图像x比例乘以-1以水平翻转图像:

let image = UIImage(data: try! Data(contentsOf: URL(string:"https://i.stack.imgur.com/Xs4RX.jpg")!))!
let transform = CGAffineTransform(scaleX: -1, y: 1)
if let ciimage = CIImage(image: image),
    let flipped = CIFilter(name: "CIAffineTransform", withInputParameters: [kCIInputImageKey: ciimage, kCIInputTransformKey: NSValue(cgAffineTransform: transform)])?.outputImage {
    let image = UIImage(ciImage: flipped)
    UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
    image.draw(in: CGRect(origin: .zero, size: image.size))
    let flippedUIImage =  UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}