2010-08-13 120 views
1

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,但不能在中心点。如何在图像中心旋转UIImage

请帮帮我。

回答

2

使用核心动画。你可以说

UIView.layer.anchor = aPoint;

然后,使用CA框架预旋转,它将围绕该锚点发生。

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