Après avoir chargé une image de l'appareil, je dois la faire pivoter de 37,8 degrés puis l'afficher sur une vue.UIImage iPhone Rotation 37,8 degrés
Existe-t-il une fonction dans Objective-C qui peut faire la rotation de l'image?
Ian
Après avoir chargé une image de l'appareil, je dois la faire pivoter de 37,8 degrés puis l'afficher sur une vue.UIImage iPhone Rotation 37,8 degrés
Existe-t-il une fonction dans Objective-C qui peut faire la rotation de l'image?
Ian
Pour faire pivoter la vue:
imageView.transform = CGAffineTransformMakeRotation(37.8°);
Pour faire pivoter l'image,
CGContext
par UIGraphicsBeginImageContext
.CGContextRotateCTM(UIGraphicsGetCurrentContext(), 37.8°);
[yourImage drawAtPoint:...];
UIGraphicsGetImageFromCurrentImageContext();
et utiliser cette image à la place.Oui, voir ma réponse à cette question: Question about rotating a slider
Pour convertir des degrés en radians (pour les positionInRadians arg) utiliser cette fonction:
CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI/180;};
J'ai besoin de faire pivoter juste l'image, pas la vue. Est-ce possible? –
Vous ne pouvez pas simplement charger l'image dans un UIImageView et l'ajouter comme sous-vue? Sinon, je vois que KennyTM a répondu comment prendre un UIImage et faire une rotation pour obtenir un autre UIImage. – cidered
Pour faire pivoter l'image, essayez ceci:
-(IBAction)rotateImageClick:(id)sender{
UIImage *image2=[[UIImage alloc]init];
image2 = [self imageRotatedByDegrees:self.roateImageView.image deg:(90)]; //Angle by 90 degree
self.roateImageView.image = image2;
imgData= UIImageJPEGRepresentation(image2,0.9f);
}
Cette méthode vous permet de faire pivoter une image une quantité arbitraire:
- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI/180);
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, (degrees * M_PI/180));
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width/2, -oldImage.size.height/2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Voir this link pour plus d'informations.
Bonne explication. Merci – Biranchi