如何水平翻转UIImage?

pra*_*epa 109 uiimage ios uiimageorientation

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

aro*_*oth 230

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

UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage 
                                            scale:sourceImage.scale
                                      orientation:UIImageOrientationUpMirrored];
Run Code Online (Sandbox Code Playgroud)

  • 这个答案有两个问题 - 视网膜可竞争图像的尺度不是1.0,出于某种原因,`UIImageOrientationUp`工作,而'UIImageOrientationUpMirrored`没有翻转它.这工作 - `image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUp] (14认同)
  • 最好使用`sourceImage.scale`作为比例. (6认同)
  • 当我尝试做`[flippedImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]`时,这似乎完全破裂了.知道为什么吗? (3认同)

A. *_*dam 69

一个非常简单的方法是通过创建UIImageView而不是UIImage并在UIImageView上进行转换.

yourImageView.image =[UIImage imageNamed:@"whatever.png"];
yourImageView.transform = CGAffineTransform(scaleX: -1, y: 1); //Flipped
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 对于我而言,这比"UIImage"操作更好用,我发现当与`UIImageRenderingModeAlwaysTemplate`渲染模式结合使用时,它会产生副作用. (2认同)
  • 感谢您分享这一点.我对这个与这篇文章相关的难题的"答案"没有运气,但你的答案非常好,而且只有一行代码. (2认同)
  • 如果要重置翻转,请使用`yourImageView.transform = CGAffineTransformIdentity` (2认同)

Ale*_*sov 35

垂直翻转通常需要使用初始化OpenGL纹理glTexImage2d(...).上面提出的技巧实际上并没有修改图像数据,在这种情况下不起作用.这是一个代码,用于实现/sf/answers/1253656071/启发的实际数据翻转

- (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;
}
Run Code Online (Sandbox Code Playgroud)

  • 要解决质量问题,请使用UIGraphicsBeginImageContextWithOptions(image.size,NO,image.scale); 代替. (3认同)

aba*_*net 13

我尝试过使用imageFlippedForRightToLeftLayoutDirection,并创建一个具有不同方向的新UIImage,但至少这是我找到的翻转图像的唯一解决方案

let ciimage: CIImage = CIImage(CGImage: imagenInicial.CGImage!)
let rotada3 = ciimage.imageByApplyingTransform(CGAffineTransformMakeScale(-1, 1))
Run Code Online (Sandbox Code Playgroud)

正如你在我的操场上看到的那样有效!! :) 在此输入图像描述

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


Hen*_*nry 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
};
Run Code Online (Sandbox Code Playgroud)

我在更多情况下做了一些改进,例如从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];
Run Code Online (Sandbox Code Playgroud)


小智 9

这里的版本很快:(我在评论中看到了这个问题)

let srcImage = UIImage(named: "imageName")
let flippedImage = UIImage(CGImage: srcImage.CGImage, scale: srcImage.scale, orientation: UIImageOrientation.UpMirrored)
Run Code Online (Sandbox Code Playgroud)


cod*_*tte 7

这是一个水平镜像/翻转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?
}
Run Code Online (Sandbox Code Playgroud)


Sat*_*aja 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];
Run Code Online (Sandbox Code Playgroud)


Sha*_*yag 5

对于Swift 3/4:

imageView.transform = CGAffineTransform(scaleX: -1, y: 1)
Run Code Online (Sandbox Code Playgroud)


Rom*_*kin 5

iOS 10以上

[myImage imageWithHorizontallyFlippedOrientation];
Run Code Online (Sandbox Code Playgroud)

斯威夫特4:

let flippedImage = myImage.withHorizontallyFlippedOrientation()
Run Code Online (Sandbox Code Playgroud)