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?
Respuesta
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
¿No 'M_PI/2' causará una conversión de tipo no deseado en una pérdida de precisión int y posterior? – eremzeit
Encontré que esto giró mi imagen 45 grados cuando se usa en una vista normal – hdost
'Doble' no es convertible a 'CGFloat' - Obtuve este error cell.arrowImageView? .transform = CGAffineTransformMakeRotation (CGFloat (M_PI_2)) - force type cast fix –
- (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;
}
No funciona con un número arbitrario de grados. –
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
O más fácil, use UIGraphicsBeginImageContextWithOptions (rotatedSize, NO, image.scale) en lugar de UIGraphicsBeginImageContext() – Borzh
sólo tiene que añadir el código de la imagen.
Image.transform=CGAffineTransformMakeRotation(M_PI/2);
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
este código no funciona para la imagen en modo retrato por primera vez. –
Para que funcione el código anterior, también debe agregar: CGFloat DegreesToRadians estáticos (grados CGFloat) {grados de retorno * M_PI/180;}; – ddiego
@Vishu Como sé que toma la memoria respectiva al tamaño de la imagen y después de hecho soltó la memoria ocupada –
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));
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;
}
Swift 3 Versión:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
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))
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))
Swift 4+.
arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2)
.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:
- 1. Rotar una matriz bidimensional en 90 grados
- 2. Cómo rotar TextView 90 grados y mostrar
- 3. ¿Cómo rotar una matriz N x N en 90 grados?
- 4. Rotar un mapa de bits 90 grados
- 5. ¿Cómo rotar la imagen mediante programación en 90 grados en iPhone?
- 6. ¿Girando un CALayer 90 grados?
- 7. Writeablebitmap.SaveJpeg está girando mi imagen -90 grados
- 8. Lossless JPEG ¿Girar (90/180/270 grados) en Java?
- 9. ¿Cómo rotar una matriz 90 grados sin usar ningún espacio extra?
- 10. Cómo rotar las etiquetas del eje x 90 grados en el diagrama de nivel
- 11. ¿La manera más fácil de girar 90 grados una imagen usando OpenCV?
- 12. ¿Cómo rotar una imagen gradualmente en Swing?
- 13. ¿Cómo recortar y rotar la imagen mediante programación en Android?
- 14. Rotar matriz 2D en 45 grados
- 15. Android gira mapa de bits 90 grados resultados en imagen aplastada. Necesita una verdadera rotación entre vertical y horizontal
- 16. ¿Rotar una parcela en MATLAB?
- 17. Rotar imagen (es) OnClick con jQuery?
- 18. UIImagePNGR ¿Problemas con la presentación?/Imágenes giradas 90 grados
- 19. imagen presionada desde el iPhone en el modo vertical se gira 90 grados
- 20. Cómo rotar la imagen con jQuery?
- 21. Comparando la imagen en cuántos grados inclinados
- 22. ¿Cómo giro UIImageView 90 grados dentro de UIScrollView con el tamaño de imagen correcto y el desplazamiento?
- 23. Cómo rotar mi aplicación 180 grados boca abajo al girar el dispositivo 180 grados al revés?
- 24. ¿Es posible rotar programáticamente la vista en 180 grados?
- 25. en C# ¿cómo rotar picturebox con su imagen?
- 26. Android get Orientación de una cámara Bitmap? Y gire hacia atrás -90 grados
- 27. Zxing - Cambio de la vista de la cámara -90 grados
- 28. Girar una sola página 90 grados con iTextSharp/VB en un PDF de varias páginas existente
- 29. Girar una página 180 grados en LaTeX
- 30. ¿Cómo activar una función cuando el iPhone se gira 90 grados?
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) –
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) –