如何水平翻转UIImage
,我在UIImageOrientationUpMirrored
类引用中找到了UIImage
枚举值,如何使用此属性来翻转UIImage
。
答案 0 :(得分:227)
UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"];
UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage
scale:sourceImage.scale
orientation:UIImageOrientationUpMirrored];
答案 1 :(得分:64)
一种非常简单的方法是通过创建UIImageView而不是UIImage并在UIImageView上进行转换。
yourImageView.image =[UIImage imageNamed:@"whatever.png"];
yourImageView.transform = CGAffineTransform(scaleX: -1, y: 1); //Flipped
希望这有帮助。
答案 2 :(得分:35)
使用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;
}
答案 3 :(得分: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];
答案 4 :(得分:10)
我尝试过使用imageFlippedForRightToLeftLayoutDirection,并创建一个具有不同方向的新UIImage,但至少这是我找到的翻转图像的唯一解决方案
let ciimage: CIImage = CIImage(CGImage: imagenInicial.CGImage!)
let rotada3 = ciimage.imageByApplyingTransform(CGAffineTransformMakeScale(-1, 1))
当然,让finalImage = UIImage(CIImage:rotada3)
答案 5 :(得分:9)
这里的版本很快:(我在评论中看到了这个问题)
let srcImage = UIImage(named: "imageName")
let flippedImage = UIImage(CGImage: srcImage.CGImage, scale: srcImage.scale, orientation: UIImageOrientation.UpMirrored)
答案 6 :(得分:5)
这是一个水平镜像/翻转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?
}
答案 7 :(得分:5)
对于Swift 3/4:
imageView.transform = CGAffineTransform(scaleX: -1, y: 1)
答案 8 :(得分: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];
答案 9 :(得分:3)
iOS 10 +
[myImage imageWithHorizontallyFlippedOrientation];
斯威夫特4:
let flippedImage = myImage.withHorizontallyFlippedOrientation()
答案 10 :(得分:2)
一个简单的扩展。
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)
答案 11 :(得分: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;
答案 12 :(得分:0)
以上修改过的答案之一,以及 Swift 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)
答案 13 :(得分:0)
aroth在 SWIFT 3中的回答
let sourceImage = UIImage(named: "whatever.png")!
let flippedImage = UIImage(cgImage: sourceImage.cgImage!, scale: sourceImage.scale, orientation: .upMirrored)
答案 14 :(得分:0)
Swift 4
yourImage.transform = CGAffineTransform(scaleX: -1, y: 1)
答案 15 :(得分:0)
由于展开,请执行以下操作:
let srcImage = UIImage(named: "myimage")!
let flippedImage = UIImage(cgImage: srcImage.cgImage!,
scale: srcImage.scale, orientation: UIImage.Orientation.upMirrored)
答案 16 :(得分:0)
您可以使用此按钮随意旋转图像
SWIFT 4
extension UIImage {
public func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage {
let radiansToDegrees: (CGFloat) -> CGFloat = {
return $0 * (180.0 / CGFloat(M_PI))
}
let degreesToRadians: (CGFloat) -> CGFloat = {
return $0 / 180.0 * CGFloat(M_PI)
}
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRect(origin: CGPoint.zero, size: size))
let t = CGAffineTransform(rotationAngle: degreesToRadians(degrees));
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap = UIGraphicsGetCurrentContext()!
bitmap.translateBy(x: rotatedSize.width / 2.0, y: rotatedSize.height / 2.0)
// Move the origin to the middle of the image so we will rotate and scale around the center.
//CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0, rotatedSize.height / 2.0);
// // Rotate the image context
bitmap.rotate(by: degreesToRadians(degrees))
// CGContextRotateCTM(bitmap, degreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
var yFlip: CGFloat
if(flip){
yFlip = CGFloat(-1.0)
} else {
yFlip = CGFloat(1.0)
}
bitmap.scaleBy(x: yFlip, y: -1.0)
//CGContextScaleCTM(bitmap, yFlip, -1.0)
bitmap.draw(self.cgImage!, in: CGRect.init(x: -size.width / 2, y: -size.height / 2, width: size.width, height: size.height))
// CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}
答案 17 :(得分:-1)
Swift 5-Xcode 11.5
水平旋转的最佳解决方案: 观看此视频:
https://m.youtube.com/watch?v=4kSLbuB-MlU
或使用此代码:
import UIKit
class FirstViewControl: UIViewController {
@IBOutlet weak var buttonAnim: UIButton!
@IBAction func ClickOnButtonAnim(_ sender: UIButton) {
UIView.transition(with: buttonAnim, duration: 0.4, options: .transitionFlipFromLeft, animation: nil , completion: nil)
}
}
您可以在此动画中使用任何ui(按钮,标签或uiview或图像)。