2011-06-14 88 views
1

我必須旋轉UIImage,然後再將它保存到文件中。我嘗試了一些方法,其中一些似乎適用於其他人,但它似乎只是將我的圖像變爲白色。這裏是我的代碼:幫助旋轉UIImage

CGFloat radians = 90.0f * M_PI/180; 
    UIGraphicsBeginImageContext(image.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextConcatCTM(ctx, CGAffineTransformMakeRotation(radians)); 
    CGContextDrawImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage); 
    image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

任何人都可以發現我做錯了什麼嗎?變量「image」是一個UIImage

回答

3
- (UIImage *)imageRotatedByRadians:(CGFloat)radians 
{ 
    // calculate the size of the rotated view's containing box for our drawing space 
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)]; 
    CGAffineTransform t = CGAffineTransformMakeRotation(radians); 
    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, radians); 

    // Now, draw the rotated/scaled image into the context 
    CGContextScaleCTM(bitmap, 1.0, -1.0); 
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2, self.size.width, self.size.height), [self CGImage]); 

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 
+0

您是否有UIImageOrientation的線程安全代碼和工作代碼,它不等於UIImageOrientationUp? – Pion 2013-04-12 09:07:14

+0

爲什麼這會改變圖像大小?有沒有辦法做到這一點,而不改變圖像大小? – Esqarrouth 2014-05-12 22:49:27

+0

你的意思是像內存大小或像素,寬度x高度的圖像大小? – drakos 2014-05-18 22:03:56

0

我相信你沒有考慮到iPhone和Quartz座標系的座標系不同。
嘗試在CGContextConcatCTM之前放置這兩行。

// to account for Quartz coordinate system. 
CGContextScaleCTM(cx, 1, -1); 
CGContextTranslateCTM(ctx, 0, -longerSide); // width or height depending on the orientation 
+0

你說得對,我沒沒有考慮到這一點,所以謝謝你。但它沒有解決問題。 – Adam 2011-06-16 04:04:54

+0

嗨,你有一些代碼來正確旋轉圖像(所有圖像方向)?謝謝 – Pion 2013-04-12 09:20:29

0

使用下面的代碼來旋轉:

UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image1.png"]]; 
[imageView setFrame:CGRectMake(0.0f, 0.0f,100.0f, 100.0f)]; 
[self.view addSubview:imageView];  
self.imageView.transform = CGAffineTransformMakeRotation((90.0f * M_PI)/180.0f); 
+1

我不想將圖像顯示在屏幕上 - 它在保存前需要旋轉。不幸的是,即使我把它放在一個圖像視圖中,並且保存該視圖的CALayer版本似乎也不能正確旋轉圖像內容。 – Adam 2011-06-15 01:07:30

0

我建議把你的形象在UIImageView,然後用下面的代碼:

- (void)setupRotatingButtons 
{ 
// call this method once; make sure "self.view" is not nil or the button 
// won't appear. the below variables are needed in the @interface. 
// center: the center of rotation 
// radius: the radius 
// time: a CGFloat that determines where in the cycle the button is located at 
//   (note: it will keep increasing indefinitely; you need to use 
//   modulus to find a meaningful value for the current position, if 
//   needed) 
// speed: the speed of the rotation, where 2 * 3.1415 is **roughly** 1 lap a 
//   second 
center = CGPointMake(240, 160); 
radius = 110; 
time = 0; 
speed = .3 * 3.1415; // <-- will rotate CW 360 degrees per .3 SECOND (1 "lap"/s) 

CADisplayLink *dl = [CADisplayLink displayLinkWithTarget:self selector:@selector(continueCircling:)]; 
[dl addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
} 

- (void)continueCircling:(CADisplayLink *)dl 
{ 
time += speed * dl.duration; 
//here rotate your image view 
yourImageView.transform = CGAffineTransformMakeRotation(time); 
}