2011-02-24 132 views
4

我想旋轉UIImage(不是UIImageView)在自定義度 我跟着this post但它不適用於我。旋轉UIImage自定義度

任何人都可以幫忙嗎?謝謝。

UPDATE: 下面的代碼做一些工作,但我旋轉之後失去一些圖像:

enter image description here

我應該改變,以得到它的權利? (順便說一句黃色的截圖是我的UIImageView BG)

- (UIImage *) rotate: (UIImage *) image 
{ 
    double angle = 20; 
    CGSize s = {image.size.width, image.size.height}; 
    UIGraphicsBeginImageContext(s); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(ctx, 0,image.size.height); 
    CGContextScaleCTM(ctx, 1.0, -1.0); 

    CGContextRotateCTM(ctx, 2*M_PI*angle/360); 
    CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width, image.size.height),image.CGImage); 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 
+0

您好它是從左修整圖像,如果我旋轉由30個左右的角度旋轉原始圖像。 – 2013-04-29 18:13:16

回答

-2

你必須做一些這樣的事

YourContainer.transform = CGAffineTransformMakeRotation(270.0/180*M_PI); 

我想的東西休息,你可以想通了..

+5

除了他想要旋轉圖像本身,而不是包含它的視圖。 – Nyx0uf 2011-02-24 09:33:25

8

該方法返回您的旋轉

#pragma mark - 
#pragma mark Rotate Image 

- (UIImage *)scaleAndRotateImage:(UIImage *)image { 

    CGImageRef imgRef = image.CGImage; 

    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 

    CGAffineTransform transform = CGAffineTransformIdentity; 
    CGRect bounds = CGRectMake(0, 0, width, height); 

    CGFloat boundHeight; 

    boundHeight = bounds.size.height; 
    bounds.size.height = bounds.size.width; 
    bounds.size.width = boundHeight; 
    transform = CGAffineTransformMakeScale(-1.0, 1.0); 
    transform = CGAffineTransformRotate(transform, M_PI/2.0); //use angle/360 *MPI 

    UIGraphicsBeginImageContext(bounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextConcatCTM(context, transform); 
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef); 
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return imageCopy; 

} 
+0

這段代碼將它旋轉90度對不對?如果我需要僅將它旋轉到5度左右,該怎麼辦? – deaa 2011-02-24 09:48:46

+1

太棒了!在autorelease模式imageCopy? – Martin 2012-07-13 10:30:28

+0

不適用於以弧度表示的自定義度數:/ – Pion 2013-04-12 16:06:47

3
- (UIImage *)rotate:(UIImage *)image radians:(float)rads 
{ 
    float newSide = MAX([image size].width, [image size].height); 
    CGSize size = CGSizeMake(newSide, newSide); 
    UIGraphicsBeginImageContext(size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(ctx, newSide/2, newSide/2); 
    CGContextRotateCTM(ctx, rads); 
    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(-[image size].width/2,-[image size].height/2,size.width, size.height),image.CGImage); 
    //CGContextTranslateCTM(ctx, [image size].width/2, [image size].height/2); 

    UIImage *i = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return i; 
} 
角度,你的圖像

這個函數在其中心旋轉任何圖像,但是圖像變成了正方形,所以我建議在該函數之後繪製時引用圖像中心。

+0

圖像旋轉但倒過來。 – Guy 2013-01-16 07:41:57

+0

圖像旋轉,但也翻轉 – Dev2rights 2013-05-15 14:38:24

2

你需要解決兩件事情來完成這項工作。

  1. 您正在繞圖像,而不是中心
  2. 所得到的圖像的邊界矩形需要較大現在圖像被旋轉以它適合在底部角落。

要解決有關中心的旋轉,首先執行平移到中心,然後旋轉,然後平移回去。

CGAffineTransform transform = CGAffineTransformIdentity; 
transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2); 
transform = CGAffineTransformRotate(transform, angle); 
transform = CGAffineTransformScale(transform, 1.0, -1.0); 

CGContextConcatCTM(context, transform); 

// Draw the image into the context 
CGContextDrawImage(context, CGRectMake(-imageView.image.size.width/2, -imageView.image.size.height/2, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage); 

// Get an image from the context 
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)]; 

要計算矩形框的大小你需要適應新的旋轉後的圖像投入使用這樣的:

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

你可以在我以前的帖子和答案在這裏找到這些技巧:

Creating a UIImage from a rotated UIImageView

這裏:

Saving 2 UIImages

希望這有助於

戴夫

+0

嗨,此代碼不適用於具有不同的方向比UIImageOrientationUp的照片,你有任何解決方案? – Pion 2013-04-12 16:18:33

0

用於旋轉圖像..你可以使用這個IBAction爲......對於每一個點擊按鈕,圖像將通過90度旋轉,可...

-(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和旋轉度以下的方法

- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees 

// ------------- -------------------------------------------------- ---------

#pragma mark - imageRotatedByDegrees Method 

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

} 

我覺得這個鏈接會有幫助.......!通過點擊按鈕目標C

http://adrianmobileapplication.blogspot.com/2015/03/rotate-original-image-by-clicking.html