我发现有几个帖子,但没有一个对我有用。
我想旋转图像(一次顺时针/逆时针旋转)。我通过以下代码完成了这项工作,但是当我将旋转的图像指定给图像视图时,每次点击后图像都会变小。
我已经调试并发现每次旋转(顺时针/逆时针)图像尺寸都会增加。我知道什么时候旋转图像然后图像尺寸略有增加,但这里的图像尺寸比预期的要大得多。
//code for image rotation
- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
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, (degrees * M_PI / 180));
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (IBAction)btnRotateImageTapped:(UIButton *)sender {
static NSInteger degree = 0;
if (sender.tag == 471) { //rotate left btn tag
degree += 1;
} else if (sender.tag == 472) { //rotate right btn tag
degree += -1;
}
UIImage *img = [self imageRotatedByDegrees:self.imgViewTeethTemplate.image deg:degree];
self.imgViewTeethTemplate.image = img;
}
我不知道这是否正确。如果没有,任何人都可以帮助我。必须得到任何帮助。
答案 0 :(得分:0)
我认为这是因为你在每次轮换时计算新的尺寸。您应该将图像复制到1 / cos(45deg)大的新图像(即2 / sqrt(2)),然后根据需要旋转此新图像。您无需担心丢失原始图像的一部分,因为无论旋转是什么,它都将包含在较大的图像中。