简体   繁体   English

如何水平翻转图像以保存翻转相册?

[英]How to flip image horizontally to save flipped in photo album?

I have been trying to flip an UIImage image horizontally so that i can save it in my photo album, everything is working fine and I am able to save the image to photo album but the image although appears flipped in my app is not saved as flipped in the photo album. 我一直试图水平翻转UIImage图像,以便我可以将它保存在我的相册中,一切正常,我可以将图像保存到相册,但图像虽然在我的应用程序中显示翻转但未保存为翻转在相册中。 I am using following code to flip the image: 我正在使用以下代码来翻转图像:

myImage?.withHorizontallyFlippedOrientation()

Note: I am not trying to flip the UIImageView , I am trying to flip the image and save it to a new image variable and assigning the image from that image variable to UIImageView . 注意:我没有尝试翻转UIImageView ,我试图翻转图像并将其保存到新的图像变量并将图像从该图像变量分配给UIImageView

Complete IBAction Code: 完整的IBAction代码:

@IBAction func horizontalFlipBtnPressed(_ sender: Any) {
    if myImageView.image != nil{
    let image = myImageView.image
    tempImage.append((image?.withHorizontallyFlippedOrientation())!)

    myImageView.image = tempImage.last



    }
    else{
        print ("there is no image selected")
    }
}

Code for saving in Photos Album: 在相册中保存的代码:

if myImageView.image != nil{
        var image = myImageView.image
        let imageData = image!.pngData()
        let compressedImage = UIImage(data: imageData!)
        UIImageWriteToSavedPhotosAlbum(compressedImage!, nil, nil, nil)
        let alert = UIAlertController(title: "Saved", message: "Your image has been saved!", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
        alert.addAction(okAction)
        self.present(alert, animated: true,completion: nil)

    }

From Apple's docs : 来自Apple的文档:

Image orientation affects the way the image data is displayed when drawn. 图像方向会影响绘制时图像数据的显示方式。

So that doesn't actually affect the image data. 这实际上并不影响图像数据。

If you just want to "flip the orientation" in the image's metadata, you can use .withHorizontallyFlippedOrientation() and then just save the image (instead of translating it to png data and back). 如果您只想在图像的元数据中“翻转方向”,可以使用.withHorizontallyFlippedOrientation() ,然后只保存图像(而不是将其转换为png数据并返回)。

If you really want the image data (the pixels) flipped, you need to draw it flipped and then save that image. 如果您真的想要翻转图像数据(像素),则需要将其翻转然后保存图像。 One way to do it: 一种方法:

func flipImageLeftRight(_ image: UIImage) -> UIImage? {

    UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
    let context = UIGraphicsGetCurrentContext()!
    context.translateBy(x: image.size.width, y: image.size.height)
    context.scaleBy(x: -image.scale, y: -image.scale)

    context.draw(image.cgImage!, in: CGRect(origin:CGPoint.zero, size: image.size))

    let newImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return newImage
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM