如何水平翻转UIImage
,我发现UIImageOrientationUpMirrored
的枚举值在UIImage
类的引用中,如何利用这个属性来翻转UIImage
。如何水平翻转UIImage?
回答
UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"];
UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage
scale:sourceImage.scale
orientation:UIImageOrientationUpMirrored];
这个答案有两个问题 - 规模不是视网膜可竞争图像的1.0,由于某些原因,'UIImageOrientationUp'工作,而'UIImageOrientationUpMirrored'没有翻转它。这工作 - 'image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUp]' – Kof 2013-06-03 16:56:09
@Kof当我注意到,您传递的方向参数用于确定'什么sourceImage的方向已经'作为反对'给我这个形象与这个特定的方向'。因此,您可以检查sourceImage.imageOrientation参数并传递不同的方向来欺骗该方法,以便为您提供所需的内容。 – 2013-06-21 10:21:48
对于scale来说,使用'sourceImage.scale'会更好。 – 2013-10-13 23:55:46
因为它形象定位定义:
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];
可能这将是使用了一段:
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];
垂直翻转,往往需要使用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;
}
此翻转图像如何?它看起来像它再次绘制图像。 – 2015-05-05 21:56:07
@Jonathan。我认为它在绘图时由于不同的坐标系(即Y轴方向)而翻转。 – 2015-05-07 09:55:20
有没有办法使用它来垂直翻转图像? – Praxiteles 2015-06-09 02:24:34
这里做灵感的实际数据翻转的SWIFT版本:(我看到在评论这个问题)
let srcImage = UIImage(named: "imageName")
let flippedImage = UIImage(CGImage: srcImage.CGImage, scale: srcImage.scale, orientation: UIImageOrientation.UpMirrored)
一个你可以做到这一点很简单的方法是通过创建一个UIImageView而不是一个UIImage,并对UIImageView进行转换。
yourImageView.image =[UIImage imageNamed:@"whatever.png"];
yourImageView.transform = CGAffineTransformMakeScale(-1, 1); //Flipped
希望这会有所帮助。
对于我来说,这最终比我的'UIImage'操作更好,当我与UIImageRenderingModeAlwaysTemplate渲染模式结合使用时,我发现它具有副作用。 – devios1 2015-06-01 23:57:30
谢谢你分享这个。我对这篇文章中的这个难题的“答案”没有任何好运,但是你的答案工作得非常好,只有1行代码。 – Adrian 2015-07-17 19:52:45
工程太棒了! iOS 9+现在还包含flipsForRightToLeftLayoutDirection,但它还不适用于iOS 8+应用程序。 – Barry 2016-06-27 23:40:30
我试图与imageFlippedForRightToLeftLayoutDirection,并创建具有diferent方向新的UIImage,但至少这是我发现我的翻转图像
let ciimage: CIImage = CIImage(CGImage: imagenInicial.CGImage!)
let rotada3 = ciimage.imageByApplyingTransform(CGAffineTransformMakeScale(-1, 1))
正如你可以在我的游乐场,它的工作看到的唯一的解决方案! :)
而且,当然,让finalImage = UIImage的(CIImage:rotada3)
这是一个工作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;
我不认为这是最好的主意。这个'imageFlippedForRightToLeftLayoutDirection'是为了与翻转的布局方向一起使用 - 例如阿拉伯国家。所以使用它可能并不总是按照需要工作。 – 2016-01-21 12:54:30
是的,你是对的 - 在我的情况下,这正是RTL支持。我们都知道SO上的代码仅供参考,并且人们不会真正只是复制/粘贴而不理解它,对吗? – capikaw 2016-01-21 16:12:48
的SWIFT 3:
imageView.transform = CGAffineTransform(scaleX: -1, y: 1)
这是一个可靠的实现,用于水平镜像/翻转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?
}
这里的上述修改和斯威夫特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)
的简单延伸。
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)
的iOS 10+
[myImage imageWithHorizontallyFlippedOrientation];
SWIFT 4
yourImage.transform = CGAffineTransform(scaleX: -1, y: 1);
仅有代码的答案通常被认为是低质量的。除了你的代码之外,还要解释如何/为什么会起作用,修复问题或回答问题。 – chharvey 2017-12-13 23:51:34
- 1. 如何水平翻转UILabel?
- 2. 水平翻转JTree
- 3. CATransform3DRotate水平翻转
- 4. SVG转换 - 水平翻转
- 5. 我如何在cocos2d中水平翻转水平对于iphone
- 6. 水平翻转图像
- 7. Android SeekBar水平翻转
- 8. iPhone - 翻转UIImage
- 9. 我旋转图像倒过来,但如何水平翻转它
- 10. 水平翻转一个帧在Objective-C
- 11. 引导模板 - 水平翻转
- 12. 2D阵列垂直和水平翻转
- 13. 水平或垂直翻转JavaFX图像
- 14. Android摄像头预览水平翻转
- 15. 模态的水平翻转动画UIView
- 16. 的JavaScript翻转文字水平
- 17. Illustrator的脚本水平翻转
- 18. HTML5 Canvas ShadowBlur和水平翻转
- 19. 撤消水平/垂直翻转位图
- 20. 在OpenCVSharp中水平翻转图像
- 21. linesegment上的水平和垂直翻转
- 22. 水平翻转画布图像?
- 23. 水平翻转缓冲图像
- 24. 在Java中水平翻转区域
- 25. 捕获的图像水平翻转
- 26. Java ME:如何水平/垂直翻转svg图像?
- 27. 如何使用JavaScript水平翻转文本?
- 28. 如何用d3.js水平翻转画布左侧的文本?
- 29. 如何水平翻转svg中的图像?
- 30. 如何只水平翻转图像而不是canvs?
为了增加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
净/博客/ 2010/03/31/iPhone-UIImage的旋转和缩放/)。奇迹般有效。 – dwbrito 2013-12-13 12:54:24