Как перевернуть UIImage
по горизонтали, я нашел значение перечисления UIImageOrientationUpMirrored
в справочнике по классу UIImage
, как использовать это свойство для переворота UIImage
.
Как перевернуть UIImage по горизонтали?
Ответы (19)
Цель-C
UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"];
UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage
scale:sourceImage.scale
orientation:UIImageOrientationUpMirrored];
Быстрый
let flippedImage = myImage.withHorizontallyFlippedOrientation()
UIImageOrientationUp
работал, а UIImageOrientationUpMirrored
не переворачивал его. Это сработало - image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUp]
- person Kof; 03.06.2013
sourceImage.scale
.
- person Sam Soffes; 14.10.2013
UIImage(CGImage: CGImage!, scale: CGFloat, orientation: UIImageOrientation)
- person irmakcanozsut; 24.10.2014
[flippedImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
. Есть идеи, почему?
- person devios1; 29.05.2015
UIImageOrientationDownMirrored
, переворачивает переданное изображение по горизонтальной оси.
- person Iulian Onofrei; 01.09.2017
Очень простой способ добиться этого — создать UIImageView вместо UIImage и выполнить преобразование в UIImageView.
yourImageView.image =[UIImage imageNamed:@"whatever.png"];
yourImageView.transform = CGAffineTransform(scaleX: -1, y: 1); //Flipped
Надеюсь это поможет.
UIImage
, которая, как я обнаружил, имела побочные эффекты в сочетании с режимом рендеринга UIImageRenderingModeAlwaysTemplate
.
- person devios1; 02.06.2015
yourImageView.transform = CGAffineTransformIdentity
- person chanil; 19.10.2016
Вертикальное отражение часто требуется для инициализации текстуры OpenGL с помощью glTexImage2d(...)
. Предложенные выше приемы на самом деле не изменяют данные изображения и не будут работать в этом случае. Вот код для фактического переворачивания данных, вдохновленный 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;
}
Я пробовал с imageFlippedForRightToLeftLayoutDirection и создавал новый UIImage с разными ориентациями, но, по крайней мере, это единственное решение, которое я нашел для переворачивания моего изображения
let ciimage: CIImage = CIImage(CGImage: imagenInicial.CGImage!)
let rotada3 = ciimage.imageByApplyingTransform(CGAffineTransformMakeScale(-1, 1))
Как вы можете видеть на моей игровой площадке, это сработало!! :)
И пусть finalImage = UIImage(CIImage: rotada3)
Поскольку это определяет ориентацию изображения:
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
};
Я сделал некоторые улучшения для других обстоятельств, таких как обработка UIImage из AVCaptureSession.
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];
вот быстрая версия: (я видел этот вопрос в комментариях)
let srcImage = UIImage(named: "imageName")
let flippedImage = UIImage(CGImage: srcImage.CGImage, scale: srcImage.scale, orientation: UIImageOrientation.UpMirrored)
Это надежная реализация для отражения/переворота 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?
}
iOS 10+
[myImage imageWithHorizontallyFlippedOrientation];
Свифт 4:
let flippedImage = myImage.withHorizontallyFlippedOrientation()
Может быть, это будет полезно для некоторых:
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];
Для Swift 3/4:
imageView.transform = CGAffineTransform(scaleX: -1, y: 1)
Простое расширение.
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)
Это рабочая версия, совместимая с 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;
imageFlippedForRightToLeftLayoutDirection
предназначен для использования с перевернутыми направлениями макета — например, для арабских стран. Поэтому использование этого не всегда может работать так, как хотелось бы.
- person Peter Stajger; 21.01.2016
Протестировано в Swift 3 и выше
Вот простое решение этой проблемы с помощью расширений. Я протестировал это, и это сработало. Вы можете отражать в любом направлении.
extension UIImage {
func imageUpMirror() -> UIImage {
guard let cgImage = cgImage else { return self }
return UIImage(cgImage: cgImage, scale: scale, orientation: .upMirrored)
}
func imageDownMirror() -> UIImage {
guard let cgImage = cgImage else { return self }
return UIImage(cgImage: cgImage, scale: scale, orientation: .downMirrored)
}
func imageLeftMirror() -> UIImage {
guard let cgImage = cgImage else { return self }
return UIImage(cgImage: cgImage, scale: scale, orientation: .leftMirrored)
}
func imageRightMirror() -> UIImage {
guard let cgImage = cgImage else { return self }
return UIImage(cgImage: cgImage, scale: scale, orientation: .rightMirrored)
}
}
Использование для этого кода
let image = #imageLiteral(resourceName: "imageName")
flipHorizontally = image.imageUpMirror()
Итак, вы можете использовать другие функции.
Вот один из приведенных выше ответов, измененный и в 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)
ответ Арота в SWIFT 3:
let sourceImage = UIImage(named: "whatever.png")!
let flippedImage = UIImage(cgImage: sourceImage.cgImage!, scale: sourceImage.scale, orientation: .upMirrored)
Свифт 4
yourImage.transform = CGAffineTransform(scaleX: -1, y: 1)
В связи с распаковкой делаем следующее:
let srcImage = UIImage(named: "myimage")!
let flippedImage = UIImage(cgImage: srcImage.cgImage!,
scale: srcImage.scale, orientation: UIImage.Orientation.upMirrored)
вы можете вращать изображение, как хотите, используя это
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
}
}
Свифт 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)
}
}
Вы можете использовать любой пользовательский интерфейс (кнопку или метку, пользовательский интерфейс или изображение) в этой анимации.