2012-07-26 21 views
32

Lo que quiero hacer es tomar una instantánea de mi cámara, lo envía a un servidor y el servidor me envía la imagen en un viewController. Si la imagen está en modo vertical, la imagen aparece bien en la pantalla; sin embargo, si la imagen se tomó en modo horizontal, la imagen aparece estirada en la pantalla (¡mientras intenta aparecer en modo retrato!). No sé cómo solucionar esto, pero supongo que una solución es primero verificar si la imagen está en modo vertical/horizontal y luego, si está en modo horizontal, rotarla 90 grados antes de mostrarla en la pantalla. Entonces, ¿cómo podría hacer eso?Cómo rotar una imagen 90 grados en iOS?

+0

posible duplicado de [iOS UIImagePickerController orientación de la imagen del resultado después de la carga] (http: // stackove rflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload) –

+0

Tuve exactamente el mismo problema y lo resolví utilizando la categoría propuesta en esta respuesta: http://stackoverflow.com/a/5427890/1327557 Gestiona todos los casos (rotaciones y espejos) –

Respuesta

95
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2); 
+0

¿No 'M_PI/2' causará una conversión de tipo no deseado en una pérdida de precisión int y posterior? – eremzeit

+0

Encontré que esto giró mi imagen 45 grados cuando se usa en una vista normal – hdost

+3

'Doble' no es convertible a 'CGFloat' - Obtuve este error cell.arrowImageView? .transform = CGAffineTransformMakeRotation (CGFloat (M_PI_2)) - force type cast fix –

10
- (UIImage *)rotateImage:(UIImage*)image byDegree:(CGFloat)degrees 
{ 
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)]; 
    CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees)); 
    rotatedViewBox.transform = t; 
    CGSize rotatedSize = rotatedViewBox.frame.size; 
    [rotatedViewBox release]; 

    UIGraphicsBeginImageContext(rotatedSize); 
    CGContextRef bitmap = UIGraphicsGetCurrentContext(); 


    CGContextTranslateCTM(bitmap, rotatedSize.width, rotatedSize.height); 

    CGContextRotateCTM(bitmap, DegreesToRadians(degrees)); 


    CGContextScaleCTM(bitmap, 1.0, -1.0); 
    CGContextDrawImage(bitmap, CGRectMake(-image.size.width, -image.size.height, image.size.width, image.size.height), [image CGImage]); 

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 
+0

No funciona con un número arbitrario de grados. –

+0

Esta respuesta reduce la resolución de la imagen, si la imagen tiene un factor de escala. Además de que esta es una gran respuesta. Agregue el código siguiente y use las variables de ancho y alto en lugar de image.size.width y image.size.height: CGFloat width = image.size.width * image.scale, height = image.size.height * image.scale; – Borzh

+0

O más fácil, use UIGraphicsBeginImageContextWithOptions (rotatedSize, NO, image.scale) en lugar de UIGraphicsBeginImageContext() – Borzh

8

sólo tiene que añadir el código de la imagen.

Image.transform=CGAffineTransformMakeRotation(M_PI/2); 
25

Este es el código completo de rotación de imagen para cualquier grado simplemente agregarlo a archivo apropiado es decir, en .m de la siguiente manera en la que desea utilizar el procesamiento de imágenes

para .m

@interface UIImage (RotationMethods) 
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees; 
@end 

@implementation UIImage (RotationMethods) 

static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI/180;}; 

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees 
{ 
    // calculate the size of the rotated view's containing box for our drawing space 
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)]; 
    CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees)); 
    rotatedViewBox.transform = t; 
    CGSize rotatedSize = rotatedViewBox.frame.size; 

    // Create the bitmap context 
    UIGraphicsBeginImageContext(rotatedSize); 
    CGContextRef bitmap = UIGraphicsGetCurrentContext(); 

    // Move the origin to the middle of the image so we will rotate and scale around the center. 
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 

    // // Rotate the image context 
    CGContextRotateCTM(bitmap, DegreesToRadians(degrees)); 

    // Now, draw the rotated/scaled image into the context 
    CGContextScaleCTM(bitmap, 1.0, -1.0); 
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2, self.size.width, self.size.height), [self CGImage]); 

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 

@end 

Este es el fragmento de código del ejemplo de apple SquareCam.

Para llamar al método anterior sólo tiene que utilizar el código de abajo

UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees]; 

Aquí el cuadrado es uno UIImage y rotationDegrees es una flote Ivar para girar la imagen que los grados

+1

este código no funciona para la imagen en modo retrato por primera vez. –

+1

Para que funcione el código anterior, también debe agregar: CGFloat DegreesToRadians estáticos (grados CGFloat) {grados de retorno * M_PI/180;}; – ddiego

+0

@Vishu Como sé que toma la memoria respectiva al tamaño de la imagen y después de hecho soltó la memoria ocupada –

6

Tengo una error al intentar esto en XCode 6.3 Swift 1.2

self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2); 

Debe probar este para forzar yeso tipo fijo:

self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)); 
0

Aquí está la versión Swift 2.2 La respuesta iOSDev (en extensión UIImage):

func degreesToRadians(degrees: CGFloat) -> CGFloat { 
    return degrees * CGFloat(M_PI)/180 
} 

func imageRotatedByDegrees(degrees: CGFloat) -> UIImage { 
    // calculate the size of the rotated view's containing box for our drawing space 
    let rotatedViewBox = UIView(frame: CGRectMake(0,0,self.size.width, self.size.height)) 
    let t = CGAffineTransformMakeRotation(self.degreesToRadians(degrees)) 
    rotatedViewBox.transform = t 
    let rotatedSize = rotatedViewBox.frame.size 

    // Create the bitmap context 
    UIGraphicsBeginImageContext(rotatedSize) 
    let bitmap = UIGraphicsGetCurrentContext() 

    // Move the origin to the middle of the image so we will rotate and scale around the center. 
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 

    // Now, draw the rotated/scaled image into the context 
    CGContextScaleCTM(bitmap, 1.0, -1.0); 
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2, self.size.width, self.size.height), self.CGImage); 

    let newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 
3

Swift 3 Versión:

imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2)) 
7

Swift 3 y Swift 4 usan .pi en su lugar

por ejemplo:

//rotate 270 degrees 
myImageView.transform = CGAffineTransform(rotationAngle: (.pi * 1.5)) 

//rotate 90 degrees 
myImageView.transform = CGAffineTransform(rotationAngle: (.pi/2)) 
2

Swift 4 | Xcode 9 sintaxis (tenga en cuenta que este código hace girar un UIImageView 90 grados hacia la derecha, no un UIImage):

myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2)) 
0

Swift 4+.

arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2) 

Note: angle is in radian

.pi = 180 grados

.pi/2 = 90 grados

Si desea agregar con la animación

@IBAction func applyTransForm(sender: UIButton) { 
     sender.isSelected = !sender.isSelected 
     UIView.animate(withDuration: 1, animations: { 
     if sender.isSelected { 
      self.arrowImageView.transform = CGAffineTransform(rotationAngle: .pi) 
     } else { 
      self.arrowImageView.transform = CGAffineTransform(rotationAngle: 0) 

     } 
     }) 
    } 

Salida:

enter image description here

Cuestiones relacionadas