【问题标题】:How to resize MKAnnotationView on iOS6?如何在 iOS6 上调整 MKAnnotationView 的大小?
【发布时间】:2012-09-28 17:52:31
【问题描述】:

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

我直接更改了 MKAnnotationView 的变换,没有运气。 MKAnnotationView 只在一瞬间调整大小(当在 MKMapView 内部进行 touch up 时,在 touch up 完成后,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 的大小吗?有人知道吗?

【问题讨论】:

  • 这里有同样的问题。我的看起来像是在闪烁,好像变换一直在为 AnnotationView 重置。如果我找到解决方案,将在此处发布。

标签: mkmapview mkannotationview


【解决方案1】:

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;

【讨论】:

  • 感谢 Erik Gross,对我很有帮助。
  • 仅供参考,getZoom 来自此处讨论的类别:stackoverflow.com/questions/7657207/…
  • 这里的比例是多少
  • 毗湿奴和@Quincy。您可以从该问题第一行指出的问题中找到比例的定义。 See the answer
  • 我已经实现了相同的,但我的图像没有调整大小。
猜你喜欢
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
相关资源
最近更新 更多