我正在尝试在我的代码中实现图像旋转并运行几个测试。由于我对旋转的执行方式不太了解,所以我只是按照我在互联网上找到的一些说明并实现了我的代码。
我发现每次旋转图像时,边缘周围的1像素的整行或列都会丢失。 (一次超过M_PI学位)
当我将图像作为UIImage对象托管时,这并不明显,但是当您将UIImage保存为文件时,您可以看到它。
这是我的测试代码链接: https://github.com/asldkfjwoierjlk/UIImageRotationTest/tree/master
我不明白这种损失发生了。我错过了什么?或者它在数学上是不可避免的?
如果你感兴趣,这是我实施的轮换方法。
- (UIImage*)rotateImg:(UIImage*)srcImg
{
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, srcImg.size.width, srcImg.size.height)];
CGFloat rotationInDegree = ROTATION_DEGREE;
CGAffineTransform t = CGAffineTransformMakeRotation(rotationInDegree);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// set opaque option to NO to leave the alpha channel intact.
// Otherwise, there's no point of saving to either png or jpg.
UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, rotatedSize.width / 2.0f, rotatedSize.height / 2.0f);
CGContextRotateCTM(context, rotationInDegree);
[srcImg drawInRect:CGRectMake(-srcImg.size.width / 2.0f, -srcImg.size.height / 2.0f, srcImg.size.width, srcImg.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
答案 0 :(得分:1)
您可以将UIImageView作为视图处理。这段代码非常适合我!
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
NSNumber *currentAngle = [rotatedViewBox.layer.presentationLayer valueForKeyPath:@"transform.rotation"];
rotationAnimation.fromValue = currentAngle;
rotationAnimation.toValue = @(50*M_PI);
rotationAnimation.duration = 50.0f; // this might be too fast
rotationAnimation.repeatCount = HUGE_VALF; // HUGE_VALF is defined in math.h so import it
[rotatedViewBox.layer addAnimation:rotationAnimation forKey:@"rotationAnimationleft"];
快乐的编码!