2011-03-23 114 views
93

如何水平翻转UIImage,我发现UIImageOrientationUpMirrored的枚举值在UIImage类的引用中,如何利用这个属性来翻转UIImage如何水平翻转UIImage?

+0

为了增加aroth很好的答案,苹果介绍了其他类型的图像取向的非常好,在[此链接](http://developer.apple.com/library/ios/documentation/uikit/由于被接受的不是我的工作,我发现[这个类别](http://www.platinumball.de/)。/ apple_ref/doc/uid/TP40006890-CH3-DontLinkElementID_2) – coco 2011-10-09 16:31:54

+0

净/博客/ 2010/03/31/iPhone-UIImage的旋转和缩放/)。奇迹般有效。 – dwbrito 2013-12-13 12:54:24

回答

214
UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"]; 

UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage 
              scale:sourceImage.scale 
             orientation:UIImageOrientationUpMirrored]; 
+13

这个答案有两个问题 - 规模不是视网膜可竞争图像的1.0,由于某些原因,'UIImageOrientationUp'工作,而'UIImageOrientationUpMirrored'没有翻转它。这工作 - 'image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUp]' – Kof 2013-06-03 16:56:09

+1

@Kof当我注意到,您传递的方向参数用于确定'什么sourceImage的方向已经'作为反对'给我这个形象与这个特定的方向'。因此,您可以检查sourceImage.imageOrientation参数并传递不同的方向来欺骗该方法,以便为您提供所需的内容。 – 2013-06-21 10:21:48

+6

对于scale来说,使用'sourceImage.scale'会更好。 – 2013-10-13 23:55:46

12

因为它形象定位定义:

typedef NS_ENUM(NSInteger, UIImageOrientation) { 
UIImageOrientationUp,   // default orientation 
UIImageOrientationDown,   // 180 deg rotation 
UIImageOrientationLeft,   // 90 deg CCW 
UIImageOrientationRight,   // 90 deg CW 
UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 
UIImageOrientationDownMirrored, // horizontal flip 
UIImageOrientationLeftMirrored, // vertical flip 
UIImageOrientationRightMirrored, // vertical flip 
}; 

我做了一些改进,更多的情况下,像AVCaptureSession处理的UIImage。

UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"]; 
UIImageOrientation flipingOrientation; 
if(sourceImage.imageOrientation>=4){ 
    flippedOrientation = sourceImage.imageOrientation - 4; 
}else{ 
    flippedOrientation = sourceImage.imageOrientation + 4; 
} 
UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage 
        scale: sourceImage.scale orientation: flipingOrientation]; 
5

可能这将是使用了一段:

UIImageOrientation imageOrientation; 

    switch (sourceImage.imageOrientation) { 
     case UIImageOrientationDown: 
      imageOrientation = UIImageOrientationDownMirrored; 
      break; 

     case UIImageOrientationDownMirrored: 
      imageOrientation = UIImageOrientationDown; 
      break; 

     case UIImageOrientationLeft: 
      imageOrientation = UIImageOrientationLeftMirrored; 
      break; 

     case UIImageOrientationLeftMirrored: 
      imageOrientation = UIImageOrientationLeft; 

      break; 

     case UIImageOrientationRight: 
      imageOrientation = UIImageOrientationRightMirrored; 

      break; 

     case UIImageOrientationRightMirrored: 
      imageOrientation = UIImageOrientationRight; 

      break; 

     case UIImageOrientationUp: 
      imageOrientation = UIImageOrientationUpMirrored; 
      break; 

     case UIImageOrientationUpMirrored: 
      imageOrientation = UIImageOrientationUp; 
      break; 
     default: 
      break; 
    } 

    resultImage = [UIImage imageWithCGImage:sourceImage.CGImage scale:sourceImage.scale orientation:imageOrientation]; 
33

垂直翻转,往往需要使用glTexImage2d(...)初始化的OpenGL纹理。上面提出的技巧并没有实际修改图像数据,在这种情况下不起作用。下面是一个代码由https://stackoverflow.com/a/17909372

- (UIImage *)flipImage:(UIImage *)image 
{ 
    UIGraphicsBeginImageContext(image.size); 
    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0.,0., image.size.width, image.size.height),image.CGImage); 
    UIImage *i = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return i; 
} 
+1

此翻转图像如何?它看起来像它再次绘制图像。 – 2015-05-05 21:56:07

+0

@Jonathan。我认为它在绘图时由于不同的坐标系(即Y轴方向)而翻转。 – 2015-05-07 09:55:20

+0

有没有办法使用它来垂直翻转图像? – Praxiteles 2015-06-09 02:24:34

7

这里做灵感的实际数据翻转的SWIFT版本:(我看到在评论这个问题)

let srcImage = UIImage(named: "imageName") 
let flippedImage = UIImage(CGImage: srcImage.CGImage, scale: srcImage.scale, orientation: UIImageOrientation.UpMirrored) 
58

一个你可以做到这一点很简单的方法是通过创建一个UIImageView而不是一个UIImage,并对UIImageView进行转换。

yourImageView.image =[UIImage imageNamed:@"whatever.png"]; 
yourImageView.transform = CGAffineTransformMakeScale(-1, 1); //Flipped 

希望这会有所帮助。

+2

对于我来说,这最终比我的'UIImage'操作更好,当我与UIImageRenderingModeAlwaysTemplate渲染模式结合使用时,我发现它具有副作用。 – devios1 2015-06-01 23:57:30

+2

谢谢你分享这个。我对这篇文章中的这个难题的“答案”没有任何好运,但是你的答案工作得非常好,只有1行代码。 – Adrian 2015-07-17 19:52:45

+0

工程太棒了! iOS 9+现在还包含flipsForRightToLeftLayoutDirection,但它还不适用于iOS 8+应用程序。 – Barry 2016-06-27 23:40:30

5

我试图与imageFlippedForRightToLeftLayoutDirection,并创建具有diferent方向新的UIImage,但至少这是我发现我的翻转图像

let ciimage: CIImage = CIImage(CGImage: imagenInicial.CGImage!) 
let rotada3 = ciimage.imageByApplyingTransform(CGAffineTransformMakeScale(-1, 1)) 

正如你可以在我的游乐场,它的工作看到的唯一的解决方案! :) enter image description here

而且,当然,让finalImage = UIImage的(CIImage:rotada3)

1

这是一个工作iOS8上/ 9兼容版本:

UIImage *image = [UIImage imageNamed:name]; 

if ([[UIApplication sharedApplication] userInterfaceLayoutDirection] == UIUserInterfaceLayoutDirectionRightToLeft) { 

    if ([image respondsToSelector:@selector(imageFlippedForRightToLeftLayoutDirection)]) { 
     //iOS9 
     image = image.imageFlippedForRightToLeftLayoutDirection; 
    } 
    else { 
     //iOS8 
     CIImage *coreImage = [CIImage imageWithCGImage:image.CGImage]; 
     coreImage = [coreImage imageByApplyingTransform:CGAffineTransformMakeScale(-1, 1)]; 
     image = [UIImage imageWithCIImage:coreImage scale:image.scale orientation:UIImageOrientationUp]; 
    } 
} 

return image; 
+0

我不认为这是最好的主意。这个'imageFlippedForRightToLeftLayoutDirection'是为了与翻转的布局方向一起使用 - 例如阿拉伯国家。所以使用它可能并不总是按照需要工作。 – 2016-01-21 12:54:30

+1

是的,你是对的 - 在我的情况下,这正是RTL支持。我们都知道SO上的代码仅供参考,并且人们不会真正只是复制/粘贴而不理解它,对吗? – capikaw 2016-01-21 16:12:48

2

的SWIFT 3:

imageView.transform = CGAffineTransform(scaleX: -1, y: 1) 
4

这是一个可靠的实现,用于水平镜像/翻转UIImage,并且可以将图像应用到背面前进。由于它改变了底层图像数据,所以绘图(如截图)也会改变。测试工作,没有质量损失。

func flipImage() -> UIImage? { 
     UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) 
     let bitmap = UIGraphicsGetCurrentContext()! 

     bitmap.translateBy(x: size.width/2, y: size.height/2) 
     bitmap.scaleBy(x: -1.0, y: -1.0) 

     bitmap.translateBy(x: -size.width/2, y: -size.height/2) 
     bitmap.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return image? 
} 
0

这里的上述修改和斯威夫特3的答案,我发现是特别有用的,当你有一个按钮,需要保持翻转图像来回之一。

func flipImage(sourceImage: UIImage,orientation: UIImageOrientation) -> UIImage { 


     var imageOrientation = orientation 

     switch sourceImage.imageOrientation { 

     case UIImageOrientation.down: 
      imageOrientation = UIImageOrientation.downMirrored; 
      break; 

     case UIImageOrientation.downMirrored: 
      imageOrientation = UIImageOrientation.down; 
      break; 

     case UIImageOrientation.left: 
      imageOrientation = UIImageOrientation.leftMirrored; 
      break; 

     case UIImageOrientation.leftMirrored: 
      imageOrientation = UIImageOrientation.left; 

      break; 

     case UIImageOrientation.right: 
      imageOrientation = UIImageOrientation.rightMirrored; 

      break; 

     case UIImageOrientation.rightMirrored: 
      imageOrientation = UIImageOrientation.right; 

      break; 

     case UIImageOrientation.up: 
      imageOrientation = UIImageOrientation.upMirrored; 
      break; 

     case UIImageOrientation.upMirrored: 
      imageOrientation = UIImageOrientation.up; 
      break; 


     } 


     return UIImage(cgImage: sourceImage.cgImage!, scale: sourceImage.scale, orientation: imageOrientation) 

    } 

用途:

imageToFlip: UIImage = flipImage(sourceImage: imageToFlip, orientation: imageToFlip.imageOrientation) 
0

的简单延伸。

extension UIImage { 

    var flipped: UIImage { 
     guard let cgImage = cgImage else { 
      return self 
     } 

     return UIImage(cgImage: cgImage, scale: scale, orientation: .upMirrored) 
    } 
} 

用法:

let image = #imageLiteral(resourceName: "imageName") 
let imageView = UIImageView(image: image.flipped) 
0

aroth的答案SWIFT 3:

let sourceImage = UIImage(named: "whatever.png")! 
let flippedImage = UIImage(cgImage: sourceImage.cgImage!, scale: sourceImage.scale, orientation: .upMirrored) 
0

的iOS 10+

[myImage imageWithHorizontallyFlippedOrientation]; 
1

SWIFT 4

yourImage.transform = CGAffineTransform(scaleX: -1, y: 1); 
+2

仅有代码的答案通常被认为是低质量的。除了你的代码之外,还要解释如何/为什么会起作用,修复问题或回答问题。 – chharvey 2017-12-13 23:51:34