How to resize MKAnnotationView on iOS6?

2019-02-07 11:25发布

Resize MKAnnotationView Image When map zooms in and out? This methord are successful on iOS5, but failed on iOS6.

I change the MKAnnotationView's transform directly, and no luck. The MKAnnotationView only resize in a flash(When touch up inside the MKMapView, after the touch up finish, the MKAnnotationView will restore the original size).

My code are below:

- (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);
    }
    }
}

Can we resize the MKAnnotationView on iOS6? Anybody know any way?

1条回答
倾城 Initia
2楼-- · 2019-02-07 11:45

Apple suggests resizing the .image property of an annotation view. In my case shown below, I looked at the UIImage that was set in the viewforAnnotation and re-scaled it to the zoom level in a 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;
查看更多
登录 后发表回答