我以这种方式向 60 个 MKMapViews 添加了一个观察者:
[mapView2.userLocation addObserver:self
forKeyPath:@"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:NULL];
...
-(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
}
}}
}
从那时起,我看到很多内存被占用。这会占用大量内存吗?如何删除 viewDidUnload 中的观察者?