帮助旋转UIImage

问题描述

| 将其保存到文件中之前,我必须旋转“ 0”。我尝试了许多方法,其中一些方法似乎对其他人也有用,但似乎所有方法都能使我的图像变白。这是我的代码
    CGFloat radians = 90.0f * M_PI/180;
    UIGraphicsBeginImageContext(image.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextConcatCTM(ctx,CGAffineTransformMakeRotation(radians));
    CGContextDrawImage(ctx,CGRectMake(0,image.size.width,image.size.height),image.CGImage);
    image = UIGraphicsGetimageFromCurrentimageContext();
    UIGraphicsEndImageContext();
谁能发现我在做什么错?变量“ image”为
UIImage
。     

解决方法

        
- (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,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.height),[self CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
    ,        我相信您没有考虑到iPhone的坐标系和Quartz坐标系不同。 尝试将这两行放在CGContextConcatCTM之前。
// to account for Quartz coordinate system.
CGContextScaleCTM(cx,1,-1);
CGContextTranslateCTM(ctx,-longerSide); // width or height depending on the orientation
    ,        建议您将图片放入
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);
}
    ,        使用以下代码旋转:
 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);