Tengo un UIImageView con una imagen. He rotado la imagen antes de visualizar estableciendo la propiedad de transformación de UIImageView en CGAffineTransformMakeRotation (ángulo) donde angle es el ángulo en radianes.Crear un UIImage desde un UIImageView girado
Quiero ser capaz de crear otro UIImage que corresponda a la versión girada que puedo ver en mi opinión.
Estoy casi allí, girando el contexto imagen me sale una imagen rotada:
- (UIImage *) rotatedImageFromImageView: (UIImageView *) imageView
{
UIImage *rotatedImage;
// Get image width, height of the bounding rectangle
CGRect boundingRect = [self getBoundingRectAfterRotation: imageView.bounds byAngle:angle];
// Create a graphics context the size of the bounding rectangle
UIGraphicsBeginImageContext(boundingRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// Rotate and translate the context
CGAffineTransform ourTransform = CGAffineTransformIdentity;
ourTransform = CGAffineTransformConcat(ourTransform, CGAffineTransformMakeRotation(angle));
CGContextConcatCTM(context, ourTransform);
// Draw the image into the context
CGContextDrawImage(context, CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);
// Get an image from the context
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
// Clean up
UIGraphicsEndImageContext();
return rotatedImage;
}
Sin embargo, la imagen no se gira alrededor de su centro. He intentado todo tipo de transformaciones concatenadas con mi rotación para que gire alrededor del centro, pero fue en vano. ¿Me estoy perdiendo un truco? ¿Es esto posible ya que estoy rotando el contexto y no la imagen?
Desesperado por hacer que esto funcione ahora, por lo que cualquier ayuda sería apreciada.
de Dave
EDITAR: Me han preguntado varias veces por mi código boundingRect, así que aquí está:
- (CGRect) getBoundingRectAfterRotation: (CGRect) rectangle byAngle: (CGFloat) angleOfRotation {
// Calculate the width and height of the bounding rectangle using basic trig
CGFloat newWidth = rectangle.size.width * fabs(cosf(angleOfRotation)) + rectangle.size.height * fabs(sinf(angleOfRotation));
CGFloat newHeight = rectangle.size.height * fabs(cosf(angleOfRotation)) + rectangle.size.width * fabs(sinf(angleOfRotation));
// Calculate the position of the origin
CGFloat newX = rectangle.origin.x + ((rectangle.size.width - newWidth)/2);
CGFloat newY = rectangle.origin.y + ((rectangle.size.height - newHeight)/2);
// Return the rectangle
return CGRectMake(newX, newY, newWidth, newHeight);
}
¿Qué es esta función 'getBoundingRectAfterRotation'? – bmdhacks
Hola @bmdhacks getBoundingRectAfterRotation es un método que he creado, por lo que no lo encontrarás en ninguna de las clases de Cocoa-Touch. Toma un rect y un ángulo como argumentos y devuelve un nuevo CGRect que abarcaría el rect original si fuera rotado por el ángulo dado. –
Hola @bmdhacks debido a la demanda popular han agregado el método en una edición a mi pregunta original. –