ios 如何水平翻转 UIImage?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5404706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to flip UIImage horizontally?
提问by pradeepa
How to flip the UIImage
horizontally, I found UIImageOrientationUpMirrored
enumeration value in the UIImage
class reference, how to make use of this property to flip UIImage
.
如何UIImage
水平翻转,我UIImageOrientationUpMirrored
在UIImage
类参考中找到了枚举值,如何利用这个属性进行翻转UIImage
。
回答by aroth
Objective-C
目标-C
UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"];
UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage
scale:sourceImage.scale
orientation:UIImageOrientationUpMirrored];
Swift
迅速
let flippedImage = myImage.withHorizontallyFlippedOrientation()
回答by A. Adam
A very simple way you can achieve this is by creating a UIImageView instead of a UIImage and do the transform on UIImageView.
实现此目的的一种非常简单的方法是创建 UIImageView 而不是 UIImage 并在 UIImageView 上进行转换。
yourImageView.image =[UIImage imageNamed:@"whatever.png"];
yourImageView.transform = CGAffineTransform(scaleX: -1, y: 1); //Flipped
Hope this helps.
希望这可以帮助。
回答by Alexey Podlasov
Vertical flip is often required to initialise OpenGL texture using glTexImage2d(...)
. The above proposed tricks do not actually modify image data and will not work in this case. Here is a code to do the actual data flip inspired by https://stackoverflow.com/a/17909372
通常需要垂直翻转来使用glTexImage2d(...)
. 上面提出的技巧实际上并没有修改图像数据,在这种情况下不起作用。下面是一个代码做的灵感来自于实际的数据翻转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;
}
回答by abanet
I have tried with imageFlippedForRightToLeftLayoutDirection, and creating a new UIImage with diferent orientations but at least this is the only solution I found for flipping my image
我尝试过使用 imageFlippedForRightToLeftLayoutDirection,并创建一个具有不同方向的新 UIImage,但至少这是我找到的唯一翻转图像的解决方案
let ciimage: CIImage = CIImage(CGImage: imagenInicial.CGImage!)
let rotada3 = ciimage.imageByApplyingTransform(CGAffineTransformMakeScale(-1, 1))
As you can see in my playground it worked!! :)
And the, of course, let finalImage = UIImage(CIImage: rotada3)
当然,让 finalImage = UIImage(CIImage: rotada3)
回答by Henry
As it Image Orientation Defines:
因为它的图像方向定义:
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
};
I made some improvements for more circumstances like handling UIImage from AVCaptureSession.
我为更多情况做了一些改进,比如从 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];
回答by RomanTsopin
here's swift version: (I saw this question in comments)
这是快速版本:(我在评论中看到了这个问题)
let srcImage = UIImage(named: "imageName")
let flippedImage = UIImage(CGImage: srcImage.CGImage, scale: srcImage.scale, orientation: UIImageOrientation.UpMirrored)
回答by code4latte
This is a solid implementation to mirror/flip an UIImage horizontally, and can apply to the image back and forth. Since it changes the underlying image data, the drawing(like, screenshot) will also change. Tested to work, no quality loss.
这是水平镜像/翻转 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?
}
回答by Roman Solodyashkin
iOS 10+
iOS 10+
[myImage imageWithHorizontallyFlippedOrientation];
Swift 4:
斯威夫特 4:
let flippedImage = myImage.withHorizontallyFlippedOrientation()
回答by Sathe_Nagaraja
May be this will be of use for some:
可能这对某些人有用:
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];
回答by Shaked Sayag
For Swift 3/4:
对于 Swift 3/4:
imageView.transform = CGAffineTransform(scaleX: -1, y: 1)