我在弄清楚是否有任何方法來鏡像圖像。例如,拍攝某人臉部的照片,然後將其切成兩半,並顯示他們的每張臉的鏡像。在CGAffineTransform函數中似乎沒有這樣的技巧。圖形專家請幫忙!!!如何鏡像來自UIImagePickerController的UIImage圖片
回答
這裏的基本「技巧」是對因子爲-1的X軸或Y軸使用縮放變換。例如,你可以使用它來創建一個「繞水平軸翻轉」改造:
CGAffineTransform transform = CGAffineTransformScale(transform, -1, 1);
然後你可以設置一個UIImageView
的transform
屬性翻轉分配的圖像,或與其他串接它轉換爲做更復雜的效果。爲了獲得您描述的確切效果,您可能需要編寫一些自定義繪圖代碼,將原始圖像繪製到上下文中,然後將翻轉的一半覆蓋在上面。這在Core Graphics中相對比較簡單。
如果你只打算支持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];
你可能會想,爲什麼與悍然長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
其實如果你的答案看起來不錯,那不是每一種情況。 例如如果你想做水平翻轉,但你的圖像方向是正確的,你必須做LeftMirrored,因爲RightMirrored會給垂直翻轉。 – AncAinu 2014-08-01 16:06:01
從您坐在座位上的角度來看,從非鏡像到鏡像從水平鏡像圖像。 (從圖像本身的角度來看,它會根據枚舉中的註釋進行翻轉)。如果這些面的方向是正確的(從圖像頂部到頂部),那麼所有從非鏡像到鏡像將水平翻轉該面(即沿着垂直軸)。 UIImageOrientation的文檔:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/#//apple_ref/c/tdef/UIImageOrientation。 – sabalaba 2015-12-15 19:51:49
我編輯我的答案,建議使用明確的switch語句。誰知道在路上會發生什麼。也許Apple會改變枚舉的順序,在這種情況下,模塊化算術版本將變得不正確。 – sabalaba 2015-12-15 20:11:46
上面的答案都不是,迴應了映射圖像一半而不是翻轉整個圖像的部分問題。混合溶液導致可能會作爲一個類別如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;
}
感謝它的工作 – ChanWarde 2017-08-03 12:13:11
- 1. UIImageView不顯示來自UIImagePickerController的圖像
- 2. 如何將背景保存爲來自uiimagepickercontroller的UIimage
- 3. 如何處理來自UIImagePickerController的多個圖像?
- 4. UIImage內的UIImagePickerController
- 5. 的UIImagePickerController總是圖片640x640圖像
- 6. 來自iPhone 4相機UIImagePickerController的UIImage HDR照片是否包含HDR信息?
- 7. UIImage翻轉和鏡像
- 8. UIImage鏡像代理怪異
- 9. UIImagePickerController前置攝像頭在預覽視圖中顯示鏡像
- 10. 來自UIImagePickerController的iPhone相機圖片橫向
- 11. 來自UIImagePickerController的錯誤版本的圖像
- 12. 翻轉的圖像是鏡像的,如何使翻轉的圖像不鏡像?
- 13. 如何正確裁剪UIImage與UIImagePickerController?
- 14. 如何路徑在C#中來自圖片庫的圖像
- 15. 圖片[UIImage的imageNamed]
- 16. PHP圖片來自多個圖像
- 17. 來自docker的捲曲ubuntu鏡像
- 18. 如何使用UIImagePickerController從照片庫中刪除圖像
- 19. 如何添加UIImagePickerController的「圖像指南」?
- 20. 如何構建Docker鏡像自己的圖像
- 21. 來自攝像頭的圖片
- 22. 來自攝像頭的全屏圖片
- 23. 來自JSon的UIImage
- 24. Ipad鏡像 - 如何鏡像不同的視圖?
- 25. 如何讓Sinopia鏡像來自registry.npmjs.org的所有npm軟件包?
- 26. 如何在刀片laravel顯示來自AWS的S3圖像5.2
- 27. Android CWAC攝像頭:來自前置攝像頭的圖像總是鏡像
- 28. 使用來自NSURL的UIImage存儲圖像
- 29. 如何在旋轉UIImage後縮放並獲得鏡像?
- 30. 如何用drawRect在UIView中繪製鏡像UIImage?
或使用CGAffineTransformMakeScale即picker.cameraViewTransform = CGAffineTransformMakeScale(-1,1)//迅速 – khunshan 2014-12-26 07:10:00
優秀!我用'myImageView.transform = CGAffineTransformMakeScale(-1,1);' – 2015-07-06 16:09:08