UIImage翻转的解决方案是使用Objective-C代码:
[UIImage imageWithCGImage:img.CGImage scale:1.0 orientation: UIImageOrientationDownMirrored]
不过,在Swift中无法使用imageWithCGImage!有没有在Swift中水平翻转图像的解决方案?谢谢!
对我来说,最简单的方法是使用UIImage
的.withHorizontallyFlippedOrientation()
实例方法,如下所示:
let flippedImage = straightImage.withHorizontallyFlippedOrientation()
简单的一行代码总是让我感到高兴 :)
大多数工厂方法在 Swift 中被转换为初始化器。只要有可能,即使类方法仍然可用,也会优先使用它们。你可以使用:
大多数工厂方法在Swift中被转换为初始化器。只要有可能,即使类方法仍然可用,也会优先使用它们。你可以使用:
init(CGImage cgImage: CGImage!, scale: CGFloat, orientation: UIImageOrientation)
使用方法如下:
var image = UIImage(CGImage: img.CGImage, scale: 1.0, orientation: .DownMirrored)
Swift 5
->Swift 5
var image = UIImage(cgImage: img.cgImage!, scale: 1.0, orientation: .downMirrored)
public extension UIImage { var flippedHorizontally: UIImage { return UIImage(CGImage: self.CGImage!, scale: self.scale, orientation: .UpMirrored) } }
- KuditUIBarButtonItem
时,其比例尺寸是正常的。但是当我使用您的方法时,图像变得巨大!注意:除了您的方法之外,我不对图像应用任何转换 - 只是将其分配给工具栏按钮项。 - Gargo在 Swift 中.... (6.3.1)
YourUIImage.transform = CGAffineTransformMakeScale(-1, 1)
这也适用于UIView
UIImage
,而是为了翻转 UIImageView
。 - Léo NatanUIView
派生而来,因此它们没有transform
属性(例如UIBarButtonItem
)。 - Gargo更改图像方向参数并不总是翻转图像。在某些情况下,必须以某种方式重新绘制图像...例如:
Swift 3
Swift 3
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
}
UIBarButtonItem
时,它会消失。这是实际翻转图像的最简解决方案。
extension UIImage {
func flipHorizontally() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: self.size.width/2, y: self.size.height/2)
context.scaleBy(x: -1.0, y: 1.0)
context.translateBy(x: -self.size.width/2, y: -self.size.height/2)
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
使用此解决方案,您可以执行以下操作:
let image = UIImage(named: "image.png")!
let newImage = image.flipHorizontally()
context.scaleBy(x: -1.0, y: 1.0)
更改为 context.scaleBy(x: 1.0, y: -1.0)
即可。 - Ali PacmanSwift 4
YOURIMAGEVIEW.transform = CGAffineTransform(scaleX: -1, y: 1)
这也许会很方便:
YOURIMAGE.imageFlippedForRightToLeftLayoutDirection()
Swift 5
如果您的用例需要在转换后保存UIImage
,则需要将其绘制到新的CGContext
中。
extension UIImage {
enum Axis {
case horizontal, vertical
}
func flipped(_ axis: Axis) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image {
let context = $0.cgContext
context.translateBy(x: size.width / 2, y: size.height / 2)
switch axis {
case .horizontal:
context.scaleBy(x: -1, y: 1)
case .vertical:
context.scaleBy(x: 1, y: -1)
}
context.translateBy(x: -size.width / 2, y: -size.height / 2)
draw(at: .zero)
}
}
}
在Swift 4中翻转图像
class ViewController: UIViewController {
var first = 1
var second = 1
@IBOutlet weak var img: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func flip1(_ sender: Any) {
if first == 1 {
img.transform = CGAffineTransform(scaleX: -1, y: 1)
first = 2
}
else if first == 2
{
img.transform = CGAffineTransform(scaleX: +1, y: 1)
first = 1
}
}
}
在SO上有几个类似的问题。我相信在这里也值得使用CoreImage
发布解决方案。
请注意:在获取最终的UIImage
时,必须先转换为CGImage
以尊重CIImage
的范围。
extension UIImage {
func imageRotated(by degrees: CGFloat) -> UIImage {
let orientation = CGImagePropertyOrientation(imageOrientation)
// Create CIImage respecting image's orientation
guard let inputImage = CIImage(image: self)?.oriented(orientation)
else { return self }
// Flip the image itself
let flip = CGAffineTransform(scaleX: 1, y: -1)
let outputImage = inputImage.transformed(by: flip)
// Create CGImage first
guard let cgImage = CIContext().createCGImage(outputImage, from: outputImage.extent)
else { return self }
// Create output UIImage from CGImage
return UIImage(cgImage: cgImage)
}
}
Cannot convert value of type 'UIImage.Orientation' to expected argument type 'UInt32'
- undefined
withHorizontallyFlippedOrientation()
之后有一种调整的方法? - Francesco D.M.