2010-06-27 50 views

回答

34

這裏的基本「技巧」是對因子爲-1的X軸或Y軸使用縮放變換。例如,你可以使用它來創建一個「繞水平軸翻轉」改造:

CGAffineTransform transform = CGAffineTransformScale(transform, -1, 1); 

然後你可以設置一個UIImageViewtransform屬性翻轉分配的圖像,或與其他串接它轉換爲做更復雜的效果。爲了獲得您描述的確切效果,您可能需要編寫一些自定義繪圖代碼,將原始圖像繪製到上下文中,然後將翻轉的一半覆蓋在上面。這在Core Graphics中相對比較簡單。

+4

或使用CGAffineTransformMakeScale即picker.cameraViewTransform = CGAffineTransformMakeScale(-1,1)//迅速 – khunshan 2014-12-26 07:10:00

+1

優秀!我用'myImageView.transform = CGAffineTransformMakeScale(-1,1);' – 2015-07-06 16:09:08

19

如果你只打算支持4.0+

UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored; 
switch (image.imageOrientation) { 
    case UIImageOrientationUp: break; 
    case UIImageOrientationDown: flippedOrientation = UIImageOrientationDownMirrored; break; 
    // ... 
} 
UIImage * flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:flippedOrientation]; 
12

你可能會想,爲什麼與悍然長switch語句煩惱呢?

? UIImage *flip = [UIImage imageWithCGImage:image.CGImage 
?          scale:image.scale 
?        orientation:(image.imageOrientation + 4) % 8]; 

如果你看看枚舉,你可以看到,模運算會做:

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 
}; 

但是這個代碼是太聰明瞭。您應該使用顯式switch語句來編寫函數。例如。

UIImageOrientation mirroredImageOrientation(UIImageOrientation orientation) { 
    switch(orientation) { 
     case UIImageOrientationUp: return UIImageOrientationUpMirrored; 
     case UIImageOrientationDown: return UIImageOrientationDownMirrored; 
     case UIImageOrientationLeft: return UIImageOrientationLeftMirrored; 
     case UIImageOrientationRight: return UIImageOrientationRightMirrored; 
     case UIImageOrientationUpMirrored: return UIImageOrientationUp; 
     case UIImageOrientationDownMirrored: return UIImageOrientationDown; 
     case UIImageOrientationLeftMirrored: return UIImageOrientationLeft; 
     case UIImageOrientationRightMirrored: return UIImageOrientationRight; 
     default: return orientation; 
    } 
} 

而且使用這樣的功能:

UIImage *flip = [UIImage imageWithCGImage:image.CGImage 
            scale:image.scale 
           orientation:mirroredImageOrientation(image.imageOrientation)]; 

我已經添加了問號表示可疑,臭代碼。類似於The Practice of Programming

+1

其實如果你的答案看起來不錯,那不是每一種情況。 例如如果你想做水平翻轉,但你的圖像方向是正確的,你必須做LeftMirrored,因爲RightMirrored會給垂直翻轉。 – AncAinu 2014-08-01 16:06:01

+0

從您坐在座位上的角度來看,從非鏡像到鏡像從水平鏡像圖像。 (從圖像本身的角度來看,它會根據枚舉中的註釋進行翻轉)。如果這些面的方向是正確的(從圖像頂部到頂部),那麼所有從非鏡像到鏡像將水平翻轉該面(即沿着垂直軸)。 UIImageOrientation的文檔:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/#//apple_ref/c/tdef/UIImageOrientation。 – sabalaba 2015-12-15 19:51:49

+0

我編輯我的答案,建議使用明確的switch語句。誰知道在路上會發生什麼。也許Apple會改變枚舉的順序,在這種情況下,模塊化算術版本將變得不正確。 – sabalaba 2015-12-15 20:11:46

5

上面的答案都不是,迴應了映射圖像一半而不是翻轉整個圖像的部分問題。混合溶液導致可能會作爲一個類別如UIImage的+鏡像使用下面的示例功能:

(UIImage *) horizontalMirror { 
    UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored; 
    switch (self.imageOrientation) { 
     case UIImageOrientationUp: break; 
     case UIImageOrientationDown: flippedOrientation = UIImageOrientationDownMirrored; break; 
    } 
    UIImage * flippedImage = [UIImage imageWithCGImage:self.CGImage scale:1.0 orientation:flippedOrientation]; 

    CGImageRef inImage = self.CGImage; 
    CGContextRef ctx = CGBitmapContextCreate(NULL, 
              CGImageGetWidth(inImage), 
              CGImageGetHeight(inImage), 
              CGImageGetBitsPerComponent(inImage), 
              CGImageGetBytesPerRow(inImage), 
              CGImageGetColorSpace(inImage), 
              CGImageGetBitmapInfo(inImage) 
              ); 
    CGRect cropRect = CGRectMake(flippedImage.size.width/2, 0, flippedImage.size.width/2, flippedImage.size.height); 
    CGImageRef TheOtherHalf = CGImageCreateWithImageInRect(flippedImage.CGImage, cropRect); 
    CGContextDrawImage(ctx, CGRectMake(0, 0, CGImageGetWidth(inImage), CGImageGetHeight(inImage)), inImage); 

    CGAffineTransform transform = CGAffineTransformMakeTranslation(flippedImage.size.width, 0.0); 
    transform = CGAffineTransformScale(transform, -1.0, 1.0); 
    CGContextConcatCTM(ctx, transform); 

    CGContextDrawImage(ctx, cropRect, TheOtherHalf); 

    CGImageRef imageRef = CGBitmapContextCreateImage(ctx); 
    CGContextRelease(ctx); 
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    return finalImage; 
} 
+0

感謝它的工作 – ChanWarde 2017-08-03 12:13:11

相關問題