gpt4 book ai didi

ios - 内存警告-MapOverlay

转载 作者:行者123 更新时间:2023-11-29 04:18:13 26 4
gpt4 key购买 nike

我对 ios 的内存使用有疑问。我已经实现了如下所示的代码,并在设备上运行了大约 10 分钟,它停止了并向我发出警告“收到内存警告”。我想知道为什么我会收到此警告。

NSArray *ants = [mapView overlays];
for(bb = 0; bb < [polygonArray count]; bb++){
int attr=[[idArray objectAtIndex:bb]floatValue];


coords = malloc(sizeof(CLLocationCoordinate2D) * [[polygonArray objectAtIndex:bb] count]);
for (int a = 0;a < [[polygonArray objectAtIndex:bb] count]; a++){
coords[a].latitude = [[[[polygonArray objectAtIndex:bb]objectAtIndex:a]objectAtIndex:0]doubleValue];
coords[a].longitude = [[[[polygonArray objectAtIndex:bb]objectAtIndex:a]objectAtIndex:1]doubleValue];
}
polygon = [[MKPolygon alloc]init];
polygon = [MKPolygon polygonWithCoordinates:coords count:[[polygonArray objectAtIndex:bb]count]];
//free(coords);
[previousPolygons addObject:polygon];

}
[mapView addOverlay:polygon];
}

}
[mapView removeOverlays:ants];

最佳答案

您似乎有更多的右括号,因此可能比您向我们展示的更多,但我已经展示了我认为可以改进您的代码的内容

NSArray *ants = [mapView overlays];
for(bb = 0; bb < [polygonArray count]; bb++){
int attr=[[idArray objectAtIndex:bb]floatValue];


coords = malloc(sizeof(CLLocationCoordinate2D) * [[polygonArray objectAtIndex:bb] count]);
for (int a = 0;a < [[polygonArray objectAtIndex:bb] count]; a++){
coords[a].latitude = [[[[polygonArray objectAtIndex:bb]objectAtIndex:a]objectAtIndex:0]doubleValue];
coords[a].longitude = [[[[polygonArray objectAtIndex:bb]objectAtIndex:a]objectAtIndex:1]doubleValue];
}
//Remove the next line because polygonWIthCoordinates creates one for you
//polygon = [[MKPolygon alloc]init];
polygon = [MKPolygon polygonWithCoordinates:coords count:[[polygonArray objectAtIndex:bb]count]];
//reinstate this line
free(coords);
[previousPolygons addObject:polygon];

}
//This is outside the for-loop so you'll only be adding the last polygon
[mapView addOverlay:polygon];
}

}
[mapView removeOverlays:ants];

关于ios - 内存警告-MapOverlay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13365310/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com