【发布时间】:2014-09-17 21:26:02
【问题描述】:
UIImage翻转的解决方案是使用Objective-C代码:
[UIImage imageWithCGImage:img.CGImage scale:1.0 orientation: UIImageOrientationDownMirrored]
然而,imageWithCGImage 在 Swift 中不可用!是否有使用 Swift 水平翻转图像的解决方案?谢谢!
【问题讨论】:
UIImage翻转的解决方案是使用Objective-C代码:
[UIImage imageWithCGImage:img.CGImage scale:1.0 orientation: UIImageOrientationDownMirrored]
然而,imageWithCGImage 在 Swift 中不可用!是否有使用 Swift 水平翻转图像的解决方案?谢谢!
【问题讨论】:
对我来说,最简单的方法是使用UIImage 的.withHorizontallyFlippedOrientation() 实例方法,如下所示:
let flippedImage = straightImage.withHorizontallyFlippedOrientation()
简单的单行字总是让我开心:)
【讨论】:
withHorizontallyFlippedOrientation()之后调整这个?
大多数工厂方法都在 swift 中转换为初始化程序。只要可用,即使类方法仍然可用,它们也是首选。您可以使用:
init(CGImage cgImage: CGImage!, scale: CGFloat, orientation: UIImageOrientation)
用法如下所示:
var image = UIImage(CGImage: img.CGImage, scale: 1.0, orientation: .DownMirrored)
斯威夫特 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) } }
在 Swift 中.... (6.3.1)
YourUIImage.transform = CGAffineTransformMakeScale(-1, 1)
这也适用于 UIView
【讨论】:
UIImage,而是用来翻转UIImageView。
在所有情况下,更改图像方向参数实际上并不是翻转图像。图像必须以某种方式重绘......例如这样:
斯威夫特 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
}
【讨论】:
斯威夫特 4
YOURIMAGEVIEW.transform = CGAffineTransform(scaleX: -1, y: 1)
【讨论】:
这是实际翻转图像的最简单解决方案
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)。
在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)
}
}
【讨论】:
斯威夫特 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)
}
}
}
【讨论】: