J'ai un UIImageView avec une image. J'ai fait pivoter l'image avant d'afficher en définissant la propriété de transformation de UIImageView à CGAffineTransformMakeRotation (angle) où angle est l'angle en radians. Je veux être capable de créer un autre UIImage qui correspond à la version pivotée que je peux voir à mon avis.Création d'un UIImage à partir d'un UIImageView pivoté
Je suis presque là, en faisant tourner le contexte d'image j'obtenir une image pivotée:
- (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;
}
Cependant, l'image ne tourne pas autour de son centre. J'ai essayé toutes sortes de transformations concaténées avec ma rotation pour l'obtenir de tourner autour du centre mais en vain. Est-ce que je manque un tour? Est-ce même possible puisque je fais tourner le contexte et non l'image? Désireux de faire ce travail maintenant, alors toute aide serait appréciée.
Dave
EDIT: On m'a demandé à plusieurs reprises pour mon code boundingRect, donc la voici:
- (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'est-ce que cette fonction 'getBoundingRectAfterRotation'? – bmdhacks
Salut @bmdhacks getBoundingRectAfterRotation est une méthode que j'ai créée, vous ne la trouverez donc pas dans les classes Cocoa-Touch. Il prend un rect et un angle comme arguments et renvoie un nouveau CGRect qui engloberait le rect d'origine s'il était tourné par l'angle donné. –
Salut @bmdhacks en raison de la demande populaire ont ajouté la méthode dans une édition à ma question initiale. –