简体   繁体   English

iPhone图像无法根据相机视图的位置正确旋转

[英]iphone image not rotating properly aacording to location upon camera view

i am developing application which point towards the particular location, i have calculate angle between current coordinate to specific coordinate and my image is rotating by that difference but image rotation is fixed means when i change my direction the image is not moved it remain fix to specific postion, i want that the image should be moved aacording to the actual position where it is located(where we want to reach) thanks for the help in advance below is my code.. 我正在开发指向特定位置的应用程序,我已经计算了当前坐标与特定坐标之间的角度,并且我的图像正以该差异旋转,但是图像旋转是固定的,这意味着当我改变方向不移动图像时,它会保持固定于特定位置位置,我希望图像应根据其所处的实际位置(我们要到达的位置)移动,这要感谢您在下面的代码提供的帮助。

-(float) angleToRadians:(float) a {
    return ((a/180)*M_PI);
}
- (float) getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{

    float fLat = [self angleToRadians:fromLoc.latitude];
    float fLng = [self angleToRadians:fromLoc.longitude];
    float tLat = [self angleToRadians:toLoc.latitude];
    float tLng = [self angleToRadians:toLoc.longitude];
    float result= atan2f(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)); 

    return RadiansToDegrees(result);
}

- (void) scanButtonTouchUpInside {
    UIImage *overlayGraphic = [UIImage imageNamed:@"GFI.jpg"];
        overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
overlayGraphicView.frame = CGRectMake(50, 50, 50, 80);
        [self addSubview:overlayGraphicView];

    float angle =[self getHeadingForDirectionFromCoordinate:currentlocation  toCoordinate:pointlocation];
    if(angle < 0.0)
        angle += 2*M_PI;
overlayGraphicView.transform=CGAffineTransformMakeRotation(angle);


}

Judging by the name of the method that is rotating the image: scanButtonTouchUpInside, I'm guessing that it is called only when a user hits a button. 从旋转图像的方法的名称来判断:scanButtonTouchUpInside,我猜测只有在用户按下按钮时才调用它。 If it is only called once, I would expect the image to only rotate once, then remain fixed. 如果只调用一次,我希望图像只旋转一次,然后保持固定。 In order to keep the image rotating, you need to put that code somewhere that is called repeatedly. 为了保持图像旋转,您需要将该代码放在重复调用的地方。 A (cheap hack) way to test this would be to add this line to the end of your - (void)scanButtonTouchUpInside method: 一种测试(便宜的方法)的方法是将此行添加到- (void)scanButtonTouchUpInside方法的- (void)scanButtonTouchUpInside

[self performSelector:@selector(scanButtonTouchUpInside) withObject:nil afterDelay:0.25f];

This will make the method call fire repeatedly. 这将使方法调用反复触发。 One thing to note though: It seems that your calculation of the angle of rotation depends on a variable called currentLocation . 不过要注意一件事:旋转角度的计算似乎取决于一个称为currentLocation的变量。 This solution will only work if you are updating currentLocation appropriately. 仅当您正确更新currentLocation此解决方案才有效。

EDIT: Just a thought, you're currently calculating the angle of rotation based on the location of the device and the location of the destination. 编辑:只是一个想法,您当前正在基于设备的位置和目标的位置来计算旋转角度。 You're not taking into account compass bearing at all. 您根本不需要考虑指南针的方位。 Is this intentional? 这是故意的吗? With this approach, your angle will never change unless the device actually moves . 使用这种方法,除非设备实际移动,否则您的角度将永远不会改变。 Any change in orientation will be ignored. 方向的任何变化都将被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM