gpt4 book ai didi

ios - 图像旋转会导致像素丢失吗?(Core Graphics)

转载 作者:行者123 更新时间:2023-11-29 11:58:27 25 4
gpt4 key购买 nike

我正在尝试在我的代码中实现图像旋转并运行几个测试。由于我不太了解如何执行旋转的数学知识,因此我只是按照我在 Internet 上找到的一些说明并实现了我的代码。

我发现每次旋转图像时,边缘周围的整行或整列 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;
}

最佳答案

您可以将 UIImageView 作为 View 来处理。这段代码非常适合我!

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

编码愉快!

关于ios - 图像旋转会导致像素丢失吗?(Core Graphics),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38092243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com