如何在iOS6上调整MKAnnotationView的大小?

时间:2012-09-28 17:52:31

标签: mkmapview mkannotationview

Resize MKAnnotationView Image When map zooms in and out?这个方法在iOS5上很成功,但在iOS6上失败了。

我直接更改了MKAnnotationView的转换,但没有运气。 MKAnnotationView仅在闪存中调整大小(当在MKMapView内部触摸时,在完成修饰后,MKAnnotationView将恢复原始大小)。

我的代码如下:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
for (id <MKAnnotation>annotation in _mapView.annotations) {
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        continue;

    // handle our custom annotations
    //
    if ([annotation isKindOfClass:[XPMKAnnotation class]])
    {
        // try to retrieve an existing pin view first
        MKAnnotationView *pinView = [_mapView viewForAnnotation:annotation];
        //resize the pin view
        double zoomLevel = [_mapView getZoomLevel];
        double scale = (1.0 * zoomLevel / 16) + 0.5;
        pinView.transform = CGAffineTransformMakeScale(scale, scale);
    }
    }
}

我们可以在iOS6上调整MKAnnotationView的大小吗?有人知道吗?

1 个答案:

答案 0 :(得分:3)

Apple建议调整注释视图的.image属性。在下面的示例中,我查看了viewforAnnotation中设置的UIImage,并将其重新缩放到UIPinchGestureRecognizer中的缩放级别

   UIImage *orangeImage = [UIImage imageNamed:@"Orange210.PNG"];
    CGRect resizeRect;
    //rescale image based on zoom level
    resizeRect.size.height = orangeImage.size.height * scale;
    resizeRect.size.width = orangeImage.size.width  * scale ;
    NSLog(@"height =  %f, width = %f, zoomLevel = %f", resizeRect.size.height, resizeRect.size.width,zoomLevel );
    resizeRect.origin = (CGPoint){0,0};
    UIGraphicsBeginImageContext(resizeRect.size);
    [orangeImage drawInRect:resizeRect];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    pinView.image = resizedImage;