MKPolygonRenderer - 严重的记忆问题

时间:2013-10-17 14:39:29

标签: objective-c ios7 mapkit mkoverlay mkpolygon

我正在使用新的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。探查器并没有抱怨内存泄漏。

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

感谢您的帮助

0 个答案:

没有答案