1

我目前在使用新的 iOS7 MKPolygonRenderer 类时遇到硬内存问题。我将问题的根源定位到一行代码:

[renderer invalidatePath];

似乎核心框架没有在这里释放内存,因此对该函数的后续调用会由于内存异常而导致应用程序崩溃。

基本上我想做的是让用户改变地图上的单个多边形覆盖。

@interface MapViewController () <MKMapViewDelegate>
{
    MKPolygonRenderer* renderer;
}
@end

@implementation MapViewController

- (id) init
{
    if ((self=[super init]) != nil)
    {
        MKMapView* viewMap = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        viewMap.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        viewMap.delegate = self;
        [self.view addSubview:viewMap];
        viewMap.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(49.391759,8.675766), MKCoordinateSpanMake(0.35, 0.35));

        // a simple polygon overlay with 4 points
        CLLocationCoordinate2D overlayCoords[4] = { {49.421247,8.607101}, {49.418121,8.745117}, {49.321094,8.734818}, {49.320199,8.613968}/*, {49.370199,8.583968} */};
        MKPolygon* overlay = [MKPolygon polygonWithCoordinates:overlayCoords count:4];
        [viewMap addOverlay:overlay];

        // the gesture recognizer which is used to alter the polygon
        UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress_recognized:)];
        longPressRecognizer.minimumPressDuration = 0.1f;
        [viewMap addGestureRecognizer:longPressRecognizer];
    }
    return self;
}

- (MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    // reuse the renderer if already existent
    if (self->renderer == nil)
    {
        NSLog(@" renderer is nil ==> creating");
        renderer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
        renderer.fillColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.4];
        renderer.strokeColor = [UIColor greenColor];
        renderer.lineWidth = 1;
   }
   else
       NSLog(@" renderer is not nil ==> reusing");
   return self->renderer;
}

- (void) longPress_recognized:(UILongPressGestureRecognizer*)sender
{
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        // begin drag
        // check if and if yes, which polygon point is touched
        // set indexOfSelectedPoint
    }
    else if (sender.state == UIGestureRecognizerStateChanged)
    {
        // drag move

        if (indexOfSelectedMapPoint == -1)
            return;
        // get the coordinate of the user touch location
        CLLocationCoordinate2D c = [self->viewMap convertPoint:[sender locationInView:self->viewMap] toCoordinateFromView:self->viewMap];
        // update the coordinate of touched polygon point
        self->renderer.polygon.points[indexOfSelectedMapPoint] = MKMapPointForCoordinate(c);

        // this line causes the trouble 
        [renderer invalidatePath];
    }
    else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled)
    {
        // drag end
        // reset states
    }
}

我的测试项目正在使用 ARC。探查器没有抱怨内存泄漏。

有没有人有类似的问题?我在这里做错了什么吗?有更好的方法吗?

我在这里先向您的帮助表示感谢

4

0 回答 0