如何水平翻转UIImage?

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

 UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"]; UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage scale:sourceImage.scale orientation:UIImageOrientationUpMirrored]; 

一个非常简单的方法,你可以实现这一点是通过创build一个UIImageView而不是一个UIImage,并对UIImageView进行转换。

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

希望这可以帮助。

垂直翻转通常需要使用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; } 

由于它的图像取向定义:

 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]; 

这里是迅捷版:(我在评论中看到了这个问题)

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

可能是这将是一些使用:

  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]; 

我已经尝试imageFlippedForRightToLeftLayoutDirection,并创build一个新的UIImage不同的方向,但至less这是我find了翻转我的形象

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

正如你可以在我的操场看到它的工作! 🙂 在这里输入图像说明

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

这是水平镜像/翻转UIImage的可靠实现,可以来回应用图像。 由于它改变了底层的图像数据,所以graphics(如截图)也会改变。 testing工作,没有质量损失。

 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? } 

这是一个工作的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; 

对于Swift 3:

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

这是上面修改的一个答案,在Swift 3中 ,我发现当你有一个button需要不断来回翻转图像时特别有用。

 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) 

一个简单的扩展。

 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) 

aroth在SWIFT 3中的回答

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