How to resize MKAnnotationView on iOS6?

泪湿孤枕 提交于 2019-11-29 16:10:13

问题


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:


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;


来源:https://stackoverflow.com/questions/12644970/how-to-resize-mkannotationview-on-ios6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!