【发布时间】:2014-07-27 00:36:10
【问题描述】:
我正在尝试旋转作为注释添加到 MKMapView 的图像。
这是代码:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation
{
if (! [annotation isKindOfClass:[IGAMapAnnotation class]])
{
//return default view if annotation is NOT of type IGAMapAnnotation...
return nil;
}
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"IGAMapAnnotation"];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"IGAMapAnnotation"];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else
{
annotationView.annotation = annotation;
}
IGAMapAnnotation *myLocation = (IGAMapAnnotation *) annotation;
// THIS IS IT!
if ([myLocation.type isEqual: @"PLANE"]) {
UIImage *planeImage = [UIImage imageNamed:@"planetracked.png"];
UIImageView *planeImageView = [[UIImageView alloc]initWithImage:planeImage];
planeImageView.transform = CGAffineTransformMakeRotation(M_PI_2);
annotationView.image = planeImageView;
}
return annotationView;
}
这显然给了我一个错误,因为 annotationView.image 应该分配一个图像而不是 UIImageView。我尝试了各种方法只旋转图像,例如:
- (UIImage *)rotateImage:(UIImage *)image onDegrees:(NSString *)heading {
double angle = [heading doubleValue];
CGSize s = {image.size.width, image.size.height};
UIGraphicsBeginImageContext(s);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 0,image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextRotateCTM(ctx, 2*M_PI*angle/360);
CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width, image.size.height),image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
它们也不起作用——地图上没有图像。
有人知道如何在 MKMapView 上旋转注释图像吗?
万分感谢!
【问题讨论】:
-
而不是
annotationView.image = planeImageView;,这绝对是错误的,试试[annotationView addSubview:planeImageView];。另请注意,如果type不是“PLANE”,则注释将不可见,因为不会设置任何图像,因此您应该添加else部分以防万一。 -
安娜。太感谢了。它成功了。图像被旋转。现在的问题是在更新注释时将其从地图中删除(在我的情况下,它们每 5 秒更新一次)。我正在使用这个:
[self.mapView removeAnnotations:mapLocations];但图像没有被删除。此外,我注意到它可能出现在地图上的不同地方,尽管坐标没有改变。我想知道为什么。非常感谢!
标签: ios uiimageview uiimage mkmapview mkannotationview