开发者

Help rotating a UIImage

I have to rotate a UIImage before I save it out to file. I've tried a number of methods, some of which seem to work for other people, but all it seems to do it turn my image solid white. Here's my code:

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

Can anyone spot what I'm doing wrong? The variable "image" is a 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,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;
}


I believe you are not taking into account that the coordinate system for iPhone and Quartz Coordinate system are different.
try putting these two lines before CGContextConcatCTM.

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


I suggest to put your image in UIImageView and then use following code:

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


Use following code to rotate:

 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜