2011-06-14 82 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); 
}