帮助旋转UIImage

时间:2011-06-14 08:13:30

标签: iphone 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

4 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

我相信你没有考虑到iPhone和Quartz Coordinate系统的坐标系是不同的 尝试将这两行放在CGContextConcatCTM之前。

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

答案 2 :(得分: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);

答案 3 :(得分: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);
}