Ios MKMapView观察者
我将通过以下方式向60个MKMapView添加一个观察者:Ios MKMapView观察者,ios,xcode,Ios,Xcode,我将通过以下方式向60个MKMapView添加一个观察者: [mapView2.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
[mapView2.userLocation addObserver:self
forKeyPath:@"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:NULL];
从那以后,我看到很多记忆被占用了。这会占用很多内存吗?
如何删除ViewDiUnload中的观察者?您真的使用60 mkMapView吗?我认为他们在消耗内存,而不是观察者。您可以使用MKMapView委托而不是观察者->-(void)mapView:(MKMapView*)mapView diddupdateuserlocation:(MKUserLocation*)userlocation它回答了如何删除观察者的问题。是否有必要使用大量地图视图?是否所有地图都放置在表视图或uiview上?是否放置在UIScrollView中
-(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([self.mapView isUserLocationVisible]) {
static dispatch_once_t once;
dispatch_once(&once, ^ {
// Code to run once
MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = 0.01;
mapRegion.span.longitudeDelta = 0.01;
[mapView setRegion:mapRegion animated: YES];
// and of course you can use here old and new location values
});
for (MKMapView *map in mapViewArray)
{
if ([map isUserLocationVisible]) {
// Code to run once
MKCoordinateRegion mapRegion;
CLLocationCoordinate2D coordinate = [[map.annotations lastObject] coordinate];
mapRegion.center = coordinate;
mapRegion.span.latitudeDelta = 0.01;
mapRegion.span.longitudeDelta = 0.01;
[map setRegion:mapRegion animated: YES];
// and of course you can use here old and new location values
}
}}
}