2010-01-11 150 views

回答

6

要旋轉視圖:

imageView.transform = CGAffineTransformMakeRotation(37.8°); 

要旋轉圖像,

  1. 計算旋轉後圖像佔用的寬度和高度。
  2. 通過UIGraphicsBeginImageContext創建CGContext
  3. CGContextRotateCTM(UIGraphicsGetCurrentContext(), 37.8°);
  4. [yourImage drawAtPoint:...];
  5. UIGraphicsGetImageFromCurrentImageContext();並使用該圖片來代替。
  6. 釋放上下文。
+0

很好的解釋。謝謝 – Biranchi

1

是的,看到我的回答這個問題:Question about rotating a slider

要度轉換爲弧度(用於positionInRadians ARG)使用此功能:

CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI/180;}; 
+0

我只需旋轉圖像,而不是視圖。那可能嗎? –

+0

你不能只將圖像加載到UIImageView並將其添加爲子視圖?否則,我會看到KennyTM已經回答瞭如何獲取UIImage並旋轉以獲得另一個UIImage。 – cidered

1

要旋轉圖像,試試這個:

-(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); 

} 

此方法允許您旋轉圖像的任意量:

- (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; 

} 

更多信息請參見this link