如何水平翻转图片保存翻转到相册?
How to flip image horizontally to save flipped in photo album?
我一直在尝试水平翻转 UIImage
图像,以便将其保存在我的相册中,一切正常,我可以将图像保存到相册,但图像虽然显示在我的应用程序中翻转未保存为相册中的翻转。我正在使用以下代码翻转图像:
myImage?.withHorizontallyFlippedOrientation()
注意:我不是在尝试翻转 UIImageView
,我是在尝试翻转图像并将其保存到一个新的图像变量并将该图像变量中的图像分配给 UIImageView
。
完整的 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")
}
}
相册保存代码:
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)
}
来自 Apple 的文档:
Image orientation affects the way the image data is displayed when drawn.
所以这实际上不会影响图像数据。
如果您只想 "flip the orientation" 在图像的元数据中,您可以使用 .withHorizontallyFlippedOrientation()
然后只保存图像(而不是将其转换为 png 数据并返回)。
如果你真的想要翻转图像数据(像素),你需要翻转绘制它然后保存那个图像。一种方法:
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
}
我一直在尝试水平翻转 UIImage
图像,以便将其保存在我的相册中,一切正常,我可以将图像保存到相册,但图像虽然显示在我的应用程序中翻转未保存为相册中的翻转。我正在使用以下代码翻转图像:
myImage?.withHorizontallyFlippedOrientation()
注意:我不是在尝试翻转 UIImageView
,我是在尝试翻转图像并将其保存到一个新的图像变量并将该图像变量中的图像分配给 UIImageView
。
完整的 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")
}
}
相册保存代码:
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)
}
来自 Apple 的文档:
Image orientation affects the way the image data is displayed when drawn.
所以这实际上不会影响图像数据。
如果您只想 "flip the orientation" 在图像的元数据中,您可以使用 .withHorizontallyFlippedOrientation()
然后只保存图像(而不是将其转换为 png 数据并返回)。
如果你真的想要翻转图像数据(像素),你需要翻转绘制它然后保存那个图像。一种方法:
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
}