2010-01-11 5 views

Répondre

6

Pour faire pivoter la vue:

imageView.transform = CGAffineTransformMakeRotation(37.8°); 

Pour faire pivoter l'image,

  1. Calculez la largeur et la hauteur qui seront occupées par l'image après la rotation.
  2. Créez un CGContext par UIGraphicsBeginImageContext.
  3. CGContextRotateCTM(UIGraphicsGetCurrentContext(), 37.8°);
  4. [yourImage drawAtPoint:...];
  5. UIGraphicsGetImageFromCurrentImageContext(); et utiliser cette image à la place.
  6. Libère le contexte.
+0

Bonne explication. Merci – Biranchi

1

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;}; 
+0

J'ai besoin de faire pivoter juste l'image, pas la vue. Est-ce possible? –

+0

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

1

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.