¿Es posible girar solo la imagen en UIImageView? Estoy buscando información al respecto, pero solo encontré información sobre cómo rotar UIImageVeiw.Girar imagen en UIImageView
Respuesta
Puede girar la imagen con el siguiente código. Nota, este utiliza una CGImageRef que se puede obtener a partir de una UIImage través
CGImageRef imageRef = [self CGImageRotatedByAngle:[image CGImage] angle:30];
Una vez que la imagen girada, puede configurar la imagen de la ImageView a su nueva imagen girada de esta manera:
UIImage* img = [UIImage imageWithCGImage: imageRef];
myImageView.image = img;
aquí es un método que va a girar el imageRef:
- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
{
CGFloat angleInRadians = angle * (M_PI/180);
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect imgRect = CGRectMake(0, 0, width, height);
CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bmContext = CGBitmapContextCreate(NULL,
rotatedRect.size.width,
rotatedRect.size.height,
8,
0,
colorSpace,
kCGImageAlphaPremultipliedFirst);
CGContextSetAllowsAntialiasing(bmContext, YES);
CGContextSetShouldAntialias(bmContext, YES);
CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
CGColorSpaceRelease(colorSpace);
CGContextTranslateCTM(bmContext,
+(rotatedRect.size.width/2),
+(rotatedRect.size.height/2));
CGContextRotateCTM(bmContext, angleInRadians);
CGContextTranslateCTM(bmContext,
-(rotatedRect.size.width/2),
-(rotatedRect.size.height/2));
CGContextDrawImage(bmContext, CGRectMake(0, 0,
rotatedRect.size.width,
rotatedRect.size.height),
imgRef);
CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
CFRelease(bmContext);
[(id)rotatedImage autorelease];
return rotatedImage;
}
Eche un vistazo a la rotación de la capa de ImageView. Tendrá que importar la biblioteca QuartzCore para que sea accesible.
myImageView.layer.transform = CGAffineTransformMakeRotation (1.5);
tuvieron que usar 'setAffineTransform' – fabian789
por favor, intente esto, funciona para mí:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatCount:10];
myImgView.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];
¡Buena suerte!
el siguiente código funciona bien con Swift:
let img = UIImage(CGImage: self.imageView.image!.CGImage, scale: 1.0, orientation: UIImageOrientation.Right)
self.imageView.image = img
self.imageView.setNeedsLayout()
Si necesita girar/voltear una imagen para fines de localización se puede utilizar lo siguiente: imageView.image = imageView.image?.imageFlippedForRightToLeftLayoutDirection()
- 1. IOS: girar una UIImageView
- 2. Girar imagen en android
- 3. Girar una imagen en java
- 4. Girar cara en la imagen
- 5. Girar imagen matemáticas (C#)
- 6. ¿Cómo puedo girar un UIImageView en 20 grados?
- 7. Girar una imagen usando css
- 8. Girar UIImageView Dependiendo de la orientación del iPhone
- 9. Girar una imagen en C/C++
- 10. UIImageView no Muestra la imagen
- 11. Extracción de imagen de UIImageView
- 12. iPhone rotación UIImageView
- 13. Imagen numpy - girar la matriz 270 grados
- 14. Android Girar imagen antes de guardar
- 15. Girar imagen * sin * cambiar el tamaño
- 16. UIImageView imagen de fondo y rotación Ayuda
- 17. ¿Cómo desplazo la imagen dentro de UIImageView?
- 18. Resize UIImageView en UITableViewCell
- 19. Girar la imagen en el centro no va suave (Monodroid)
- 20. Girar una imagen en Android sin OutOfMemoryError o downscaling
- 21. Imagen Girar animación Looping no funciona en android
- 22. Girar un pivote de imagen en el centro - Android
- 23. Animate giratoria UIImageView
- 24. ¿Cómo se puede "cascada" el patrón de imagen en UIImageView?
- 25. ¿Cómo puedo cambiar la imagen mostrada en un UIImageView programáticamente?
- 26. Guardar imagen en UIImageView para iPad Photos Library
- 27. Configurando la imagen de un UIImageView en Interface Builder
- 28. Redimensionar UIImage en UIImageView
- 29. Cuando girar una UIImageView con la propiedad de transformar, los bordes pixellate
- 30. ¿Manera cruzada del navegador para girar la imagen usando CSS?
Gracias tú. Es exactamente lo que quiero. – murzynpl
Alguien puede enfrentar resultados impredecibles como lo hice recientemente: la imagen final estaba perdiendo su tamaño original y las esquinas recortadas. Así que modifiqué levemente la función para obtener el resultado correcto. Si alguien se está preguntando mi solución está aquí: http://stackoverflow.com/questions/19219855/uiimageview-get-transformed-uiimage/19221081#19221081 – heximal
Después del tamaño de la imagen de rotación cambiará como mencionó el comentarista anterior. Hay un error menor en el código anterior. La última llamada a CGContextTranslateCTM y CGContextDrawImage debe usar ancho y alto originales en lugar de roundedRect.size.width y rounded.size.height. Una vez que haces eso, este código anterior funciona perfectamente. Motivo: se crea un ctxt con rect rotado, el origen se mueve al centro y luego se rota inversamente para que coincida con la orientación de la imagen original. En este punto, debe moverlo hacia atrás en el sistema de coordenadas de la imagen original. – matangs