【问题标题】:IOS 7 - add overlay crash appIOS 7 - 添加覆盖崩溃应用程序
【发布时间】:2013-10-02 05:44:23
【问题描述】:

我正在将我的应用迁移到 IOS 7
我有一张地图,我在这张地图上画了MKPolyLine
在 IOS 7 之前一切正常,现在应用程序崩溃。
我用新方法更改了viewForOverLay

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolyline *route = overlay;
        MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
        routeRenderer.strokeColor = [UIColor redColor];
        routeRenderer.lineWidth = 7;
        return routeRenderer;
    }
    else return nil;
}

在 ViewDidLoad 我调用:

[self performSelectorInBackground:@selector(drawPathInBackground) withObject:nil];

这是实现:

-(void)drawPathInBackground{
for(int idx = 0; idx < [routes count]; idx++)
    {
        Path *m_p = [routes objectAtIndex:idx];
        CLLocationCoordinate2D workingCoordinate;
        workingCoordinate.latitude=m_p.Latitude;
        workingCoordinate.longitude=m_p.Longitude;
        MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
        pointArr[idx] = point;
    }
    self.routeLine = [MKPolyline polylineWithPoints:pointArr count:[routes count]];
    //[self.mapView addOverlay:self.routeLine];
    //free(pointArr);
    dispatch_async(dispatch_get_main_queue(), ^{
    [self.mapView addOverlay:self.routeLine];
    free(pointArr);
});
}

在这一行:[self.mapView addOverlay:self.routeLine]; 我得到:EXC_BAD_ACCESS(code = 2, address = 0x0)

【问题讨论】:

    标签: iphone ios objective-c mkmapview ios7


    【解决方案1】:

    您不应该在后台线程上执行任何 UI 操作。仅在主线程上的 UI。

    【讨论】:

    • 使用类似 dispatch_async(dispatch_get_main_queue(), ^{ // 你的 UI 操作 }); 在主线程中调度你的 UI 操作。
    • 我在我的问题(函数结束)中更改了 -(void)drawPathInBackground{... 这是正确的方法吗?
    • @1110 不要将生成的 MKPolyline 存储在属性中。问题是这引入了竞争条件。只需为其使用局部变量(块捕获对象)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    相关资源
    最近更新 更多