如何找到图像的精确旋转值?

发布于 2024-12-14 14:50:10 字数 1755 浏览 2 评论 0原文

目前我正在使用以下代码来旋转我的图像

- (void)myImageRotate:(UIRotationGestureRecognizer *)gesture
{
     if(arr==nil)
        {
            arr=[[NSMutableArray alloc] init ];
        }

     if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged) 
        {
            currentRotation =(float) [gesture rotation];

        self.transform=CGAffineTransformRotate(self.transform,currentRotation);
        [gesture setRotation:0];
        [arr addObject:[NSNumber numberWithFloat:currentRotation]]; 
    }
    NSLog(@"Rotation Value: %f", currentRotation);

//现在我将所有旋转值保存到数组中执行反转数组并获取 //最后一个旋转值

NSArray *reversedArray = [[arr reverseObjectEnumerator] allObjects];
NSLog(@"Array: %@", arr);
NSLog(@"Reversed Array: %@", reversedArray);

//现在反转数组显示始终以 0.0 开头.....类似值,无论旋转反转数组如何:( 0, 0, “0.001174212”, “0.006030798”, “0.01210225”, “0.01215386”, “0.01220191”, “0.01224673”, “0.006139159”, “0.006149054”, “0.01850212”, “0.01237607”, )

lastRotationValue = 0.0;
for (NSString* stringValue in [arr reverseObjectEnumerator]) {
    double value = [stringValue doubleValue];
    if (value != 0.0) {

        //Note: 1 degree=0.0174532925 radian

        lastRotationValue = value;
        break;
    }
}

if (lastRotationValue != 0.0) {
    NSLog(@"My firstNonZeroValue of Rotation Degree:%f",lastRotationValue);
}
}

现在我正在将最后一个旋转值写入 xml 文件,关闭 &重新启动应用程序我可以从 XML 文件中读取最后一个精确值。

但由于最后的旋转值不是实际的旋转值,因此图像没有完美地旋转到最后的状态。

所以我也尝试过放置硬编码值和图像完美旋转。

self.transform = CGAffineTransformMakeRotation(1.57);//By 90 Degree

那么应该如何解决才能获得图像的精确旋转值呢?

Currently i am using following code to rotate my image

- (void)myImageRotate:(UIRotationGestureRecognizer *)gesture
{
     if(arr==nil)
        {
            arr=[[NSMutableArray alloc] init ];
        }

     if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged) 
        {
            currentRotation =(float) [gesture rotation];

        self.transform=CGAffineTransformRotate(self.transform,currentRotation);
        [gesture setRotation:0];
        [arr addObject:[NSNumber numberWithFloat:currentRotation]]; 
    }
    NSLog(@"Rotation Value: %f", currentRotation);

//Now i am saving all rotation value to an array & perform reverse array and fetching the //last rotation value

NSArray *reversedArray = [[arr reverseObjectEnumerator] allObjects];
NSLog(@"Array: %@", arr);
NSLog(@"Reversed Array: %@", reversedArray);

//Now the Reversed array is displaying always start with 0.0.....like value whatever may be the rotation Reversed Array: (
0,
0,
"0.001174212",
"0.006030798",
"0.01210225",
"0.01215386",
"0.01220191",
"0.01224673",
"0.006139159",
"0.006149054",
"0.01850212",
"0.01237607",
)

lastRotationValue = 0.0;
for (NSString* stringValue in [arr reverseObjectEnumerator]) {
    double value = [stringValue doubleValue];
    if (value != 0.0) {

        //Note: 1 degree=0.0174532925 radian

        lastRotationValue = value;
        break;
    }
}

if (lastRotationValue != 0.0) {
    NSLog(@"My firstNonZeroValue of Rotation Degree:%f",lastRotationValue);
}
}

Now i am writing the last rotation value to a xml file ,Close & restart app i am able to read the last exact value from XML file .

But as the last rotation value is not actual rotation value the image is not rotating perfectly to last state.

So i have also tried by putting hard coded value and the image rotating perfectly.

self.transform = CGAffineTransformMakeRotation(1.57);//By 90 Degree

So what should be the solution to get exact rotation value of image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天荒地未老 2024-12-21 14:50:10

试试这个

在 .h 里放这个东西
浮动角度;

-(void)viewDidLoad
{
[super viewDidLoad];

UIRotationGestureRecognizer *recognizer = [[[UIRotationGestureRecognizer alloc]                                          initWithTarget:self                                        action:@selector(handleRotate:)] autorelease];

[self.rotatedView addGestureRecognizer:recognizer];

}
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
    // current value is past rotations + current rotation
    float rotation = angle + -recognizer.rotation;  
    self.rotatedView.transform = CGAffineTransformMakeRotation(-rotation);

    // once the user has finsihed the rotate, save the new angle
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        angle = rotation;

        NSLog(@"Last Rotation: %f",angle);
    }
}

Try this one

In .h put this thing
float angle;

-(void)viewDidLoad
{
[super viewDidLoad];

UIRotationGestureRecognizer *recognizer = [[[UIRotationGestureRecognizer alloc]                                          initWithTarget:self                                        action:@selector(handleRotate:)] autorelease];

[self.rotatedView addGestureRecognizer:recognizer];

}
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
    // current value is past rotations + current rotation
    float rotation = angle + -recognizer.rotation;  
    self.rotatedView.transform = CGAffineTransformMakeRotation(-rotation);

    // once the user has finsihed the rotate, save the new angle
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        angle = rotation;

        NSLog(@"Last Rotation: %f",angle);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文