J'utilise MKMapView dans mon grand projet. quand je charge beaucoup d'annotations (plus de 700) et que je trace une ligne entre ces points sur MapKit, je reçois le message d'erreur xcode "Terminé en raison d'une erreur de memory" et l'application plante. vous pouvez voir sur l'image:
.
J'ajoute ligne comme ceci:
Si j'ai less de 700 annotations, cela fonctionne très bien. Je pense qu'il y a un problème de memory. Comment puis-je résoudre ce problème?
// adding annodation for (int i=0; i<[fetcher.locations count]; i++)//fetcher.locations is NSMutableArray and inside have locationInfoClass objects . locationInfoClass is hold CLLocationCoordinate2D. { locationInfoClass * loc=[fetcher2.locations objectAtIndex:i]; CLLocationCoordinate2D coordinate1; coordinate1.latitude = loc.lat; coordinate1.longitude = loc.lon; myAnnotation * ann = [[myAnnotation alloc] initWithCoordinate:annCoordinate title:@"uniqtitle" subtitle:@"uniqsubtitle"]; [mapView addAnnotation:ann]; } #pragma mark - #pragma mark -mapview overlay CLLocationCoordinate2D *coordinates = malloc(sizeof(CLLocationCoordinate2D) * [mapView.annotations count]); for (int i=0; i<[mapView.annotations count]; i++) { myAnnotation * ann=[mapView.annotations objectAtIndex:i]; coordinates[i]=ann.coordinate; } self.routeLine = [MKPolyline polylineWithCoordinates:coordinates count:mapView.annotations.count]; // pinlerin sayısı ne kadarsa o kadar çizgi çiziyor. free(coordinates); [self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible dispatch_async(dispatch_get_main_queue(), ^{ [self.mapView addOverlay:self.routeLine]; });
file .h ont
@property (nonatomic, retain) MKPolyline *routeLine; //your line @property (nonatomic, retain) MKPolylineView *routeLineView; //overlay view
Méthodes MKMapView Delegate.
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay { if(overlay == self.routeLine) { if(nil == self.routeLineView) { self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine]; self.routeLineView.fillColor = [UIColor redColor]; self.routeLineView.strokeColor = [UIColor redColor]; self.routeLineView.lineWidth = 3; } return self.routeLineView; } return nil; } /* - (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views { MKAnnotationView *annotationView = [views objectAtIndex:0]; id <MKAnnotation> mp = [annotationView annotation]; MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500); [mv setRegion:region animated:YES]; }*/ - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { MKAnnotationView *pinView = nil; if(annotation != mapView.userLocation) { static NSSsortingng *defaultPinID = @"ftffggf"; pinView = (MKAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; if ( pinView == nil ) pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID]; //pinView.pinColor = MKPinAnnotationColorGreen; pinView.canShowCallout = YES; //pinView.animatesDrop = YES; UIImage * image=[UIImage imageNamed:@"pin.png"]; CGSize size=CGSizeMake(50, 63);//set the width and height pinView.image = image } else { [self.mapView.userLocation setTitle:@"I am here"]; } pinView.centerOffset = CGPointMake(0,-23); return pinView; }
Récemment – et avec une fréquence croissante – j'ai vu le même message d'erreur («Terminé à cause d'une erreur de memory») chaque fois que je courais un projet depuis XCode, après presque exactement une minute d'exécution, même si je ne touchais pas l'application Après ça a commencé.
Je ne voyais pas :
En outre, j'ai seulement vu l'application exister de manière inattendue lorsque l'application est en cours d'exécution dans le debugging – pas de return random à Springboard si j'ai ré-exécuté l'application à partir de l'appareil directement après sa fin.
J'étais sur le sharepoint poser une question similaire, détaillant tous ces détails et demandant comment diable je pouvais résoudre le problème.
Puis j'ai eu un moment d'oh, et j'ai remarqué deux avertissements de memory dans la console, même si le profileur n'affichait aucun problème de memory.
Des morts-vivants. Lorsque j'ai désactivé les objects Zombie, les avertissements de memory ont disparu et l'application ne s'est plus interrompue spontanément.
Modifier:
10 mois plus tard, j'ai trouvé une autre situation où cela peut arriver. Retourné, une boucle infinie peut aussi faire ceci:
while (result==nil) { result = [collectionView indexPathForItemAtPoint:testPoint]; testPoint = nextTestPoint(); // After about 12 seconds, at which point this is several tens of thousands of pixels off the edge of the screen, the app dies from "Memory error" }