旋转注释自定义图像

时间:2015-05-06 05:13:16

标签: ios uiimageview mkannotation

我显然没有得到这个。我试图找出如何旋转地图注释的自定义图像。即指向不同方向的多个图标。我已经对一大堆飞机数据进行了循环,我想简单地显示飞机前进的方向。

查看我已经设法拼凑起来的代码,以使至少一半的代码工作,并就如何根据变量转换图像提供一个暗示性的想法。

循环数据的摘要视图

             NSMutableArray *locations = [[NSMutableArray alloc]init];
             CLLocationCoordinate2D location;

             MKPointAnnotation *myAnn;
             myAnn = [[MKPointAnnotation alloc]init];
             location.latitude = nLatitudeFloat;
             location.longitude = nLongitudeFloat;
             myAnn.coordinate = location;
             myAnn.title = nRealname;

             //Add the Annotation object to the list so when the map is presented all points are listed on the map.
             [locations addObject:myAnn];

             //[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
             [self.mapView addAnnotations:locations];

更新注释:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // If it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]]) return nil;

    // Handle any custom annotations.
    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        // Try to dequeue an existing pin view first.
        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];

        if (!pinView)
        {
            // If an existing pin view was not available, create one.
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.canShowCallout = YES;

            pinView.image = [UIImage imageNamed:@"airplane21.png"] ;
            pinView.calloutOffset = CGPointMake(0, 32);
            pinView.transform = CGAffineTransformMakeRotation(30);    <------AT THE MOMENT I HAVE JUST HARD CODED THIS TO SEE IF IT WORKS. IT ROTATES THE ENTIRE ANNOTATION

            // Add a detail disclosure button to the callout.
            //UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            //pinView.rightCalloutAccessoryView = rightButton;

            // Add an image to the left callout.
            UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"airplane21.png"]];
            pinView.leftCalloutAccessoryView = iconView;
            iconView.transform = CGAffineTransformMakeRotation(30);  
        } else {
            pinView.annotation = annotation;
        }
        return pinView;
    }
    return nil;
}

有什么想法吗?

我顺便看过这个。我只是不自信地知道它是否适合。 Rotate annotation image on MKMapView

1 个答案:

答案 0 :(得分:1)

我有同样的问题,我有一个似乎有用的解决方案。

您需要先制作一个可以保存所需数据的自定义注释(例如坐标,您的平面标题)。循环浏览数据时,请确保使用该自定义注释。例如

CustomAnnotation* annotation = [[CustomAnnotation alloc] init];
annotation.coordinates = ...
annotation.bearing = ...

然后在您的viewForAnnotation方法中,您可以通过执行类似

的操作来获取该信息
if ([annotation isKindOfClass:[CustomAnnotation class]]) {
    CustomAnnotation* myAnn = (CustomAnnotation*)annotation;
    double bearing = myAnn.bearing; // or whatever it's called

    ...
}

希望这会有所帮助。

编辑:为了旋转图像,我在某处找到了以下代码段。它可以工作,但它会对你的图像进行像素化处理。

@interface UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
@end

@implementation UIImage (RotationMethods)

static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
    // calculate the size of the rotated view's containing box for our drawing space
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
    rotatedViewBox.transform = t;
    CGSize rotatedSize = rotatedViewBox.frame.size;

    // Create the bitmap context
    //UIGraphicsBeginImageContext(rotatedSize); // For iOS < 4.0
    UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, 0.0);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    // Rotate the image context
    CGContextRotateCTM(bitmap, DegreesToRadians(degrees));

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

@end

坚持使用.m和任何UIImage,现在可以{.1}}

现在,在您的[someUIImage imageRotatedByDegrees:yourDegrees];方法中,您可以执行类似

的操作
viewForAnnotation
相关问题