我在我的大项目中使用MKMapView。当我加载很多注释(超过700)并且我在MapKit上的这些点之间画线时,我收到xcode错误消息“由于内存错误而终止”并且应用程序正在崩溃。 你可以在图像上看到:
我正在添加这样的行:
如果我的注释少于700,那么它的效果非常好。我在想它有一些记忆问题。我怎么能解决这个问题呢?任何建议。
// 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];
});
.h文件
@property (nonatomic, retain) MKPolyline *routeLine; //your line
@property (nonatomic, retain) MKPolylineView *routeLineView; //overlay view
MKMapView委托方法。
-(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 NSString *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;
}
答案 0 :(得分:5)
最近 - 并且频率越来越高 - 我每次运行XCode项目时都会看到相同的错误消息(&#34;由于内存错误和#34而终止;)在运行几乎一分钟之后,甚至如果应用程序在启动后没有触摸它。
我不看到了:
此外,我只有 看到应用程序在调试中运行时意外存在应用程序 - 如果我在终止应用后直接从设备重新运行应用程序,则不会随机跳回Springboard
我正要问一个类似的问题,详细说明所有这些细节,并询问我究竟能如何解决这个问题。
然后我有一个时刻,并注意到控制台中有两个内存警告,即使分析器没有显示任何内存问题。
僵尸。当我关闭Zombie Objects时,内存警告消失了,应用程序不再自发终止。
修改强>
10个月后,我发现了另一种可能发生这种情况的情况。原来,无限while
循环也可以这样做:
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"
}