如何在图像中心旋转UIImage

时间:2010-08-13 05:25:48

标签: iphone uiimage


double angle = -15;
UIImage *image = [UIImage imageNamed:@"test.jpg"];
CGSize s = {image.size.width, image.size.height};
UIGraphicsBeginImageContext(s);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 0,image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextRotateCTM(ctx, 2*M_PI*angle/360);
CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width, image.size.height),image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

嗨,我不会在图像中心点旋转UIImage。我可以使用此代码旋转UIImage,但不能在中心点旋转。

请帮帮我。

2 个答案:

答案 0 :(得分:2)

使用核心动画。你可以说

UIView.layer.anchor = Point;

然后,使用CA框架执行轮换,它将围绕该锚点发生。

答案 1 :(得分:0)

此代码可以使用但只能使用UIImageOrientation = 0,

// 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, rad(degrees));

// 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]);