2011-03-23 136 views
93

如何水平翻轉UIImage,我發現UIImageOrientationUpMirrored的枚舉值在UIImage類的引用中,如何利用這個屬性來翻轉UIImage如何水平翻轉UIImage?

+0

爲了增加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

+0

淨/博客/ 2010/03/31/iPhone-UIImage的旋轉和縮放/)。奇蹟般有效。 – dwbrito 2013-12-13 12:54:24

回答

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

UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage 
              scale:sourceImage.scale 
             orientation:UIImageOrientationUpMirrored]; 
+13

這個答案有兩個問題 - 規模不是視網膜可競爭圖像的1.0,由於某些原因,'UIImageOrientationUp'工作,而'UIImageOrientationUpMirrored'沒有翻轉它。這工作 - 'image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUp]' – Kof 2013-06-03 16:56:09

+1

@Kof當我注意到,您傳遞的方向參數用於確定'什麼sourceImage的方向已經'作爲反對'給我這個形象與這個特定的方向'。因此,您可以檢查sourceImage.imageOrientation參數並傳遞不同的方向來欺騙該方法,以便爲您提供所需的內容。 – 2013-06-21 10:21:48

+6

對於scale來說,使用'sourceImage.scale'會更好。 – 2013-10-13 23:55:46

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

垂直翻轉,往往需要使用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; 
} 
+1

此翻轉圖像如何?它看起來像它再次繪製圖像。 – 2015-05-05 21:56:07

+0

@Jonathan。我認爲它在繪圖時由於不同的座標系(即Y軸方向)而翻轉。 – 2015-05-07 09:55:20

+0

有沒有辦法使用它來垂直翻轉圖像? – Praxiteles 2015-06-09 02:24:34

7

這裏做靈感的實際數據翻轉的SWIFT版本:(我看到在評論這個問題)

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

一個你可以做到這一點很簡單的方法是通過創建一個UIImageView而不是一個UIImage,並對UIImageView進行轉換。

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

希望這會有所幫助。

+2

對於我來說,這最終比我的'UIImage'操作更好,當我與UIImageRenderingModeAlwaysTemplate渲染模式結合使用時,我發現它具有副作用。 – devios1 2015-06-01 23:57:30

+2

謝謝你分享這個。我對這篇文章中的這個難題的「答案」沒有任何好運,但是你的答案工作得非常好,只有1行代碼。 – Adrian 2015-07-17 19:52:45

+0

工程太棒了! iOS 9+現在還包含flipsForRightToLeftLayoutDirection,但它還不適用於iOS 8+應用程序。 – Barry 2016-06-27 23:40:30

5

我試圖與imageFlippedForRightToLeftLayoutDirection,並創建具有diferent方向新的UIImage,但至少這是我發現我的翻轉圖像

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

正如你可以在我的遊樂場,它的工作看到的唯一的解決方案! :) enter image description here

而且,當然,讓finalImage = UIImage的(CIImage:rotada3)

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; 
+0

我不認爲這是最好的主意。這個'imageFlippedForRightToLeftLayoutDirection'是爲了與翻轉的佈局方向一起使用 - 例如阿拉伯國家。所以使用它可能並不總是按照需要工作。 – 2016-01-21 12:54:30

+1

是的,你是對的 - 在我的情況下,這正是RTL支持。我們都知道SO上的代碼僅供參考,並且人們不會真正只是複製/粘貼而不理解它,對嗎? – capikaw 2016-01-21 16:12:48

2

的SWIFT 3:

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

這是一個可靠的實現,用於水平鏡像/翻轉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? 
} 
0

這裏的上述修改和斯威夫特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) 
0

的簡單延伸。

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) 
0

aroth的答案SWIFT 3:

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

的iOS 10+

[myImage imageWithHorizontallyFlippedOrientation]; 
1

SWIFT 4

yourImage.transform = CGAffineTransform(scaleX: -1, y: 1); 
+2

僅有代碼的答案通常被認爲是低質量的。除了你的代碼之外,還要解釋如何/爲什麼會起作用,修復問題或回答問題。 – chharvey 2017-12-13 23:51:34