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

时间:2011-11-09 13:44:56

标签: iphone ipad uiimageview image-rotation

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

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

//现在Reversed数组显示总是以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文件,Close&重启应用程序我能够从XML文件中读取最后一个确切的值。

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

所以我也试过把硬编码值和图像完美地旋转。

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

那么应该是获得图像精确旋转值的解决方案。

1 个答案:

答案 0 :(得分:1)

试试这个

在.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);
    }
}