【发布时间】:2016-09-13 11:31:47
【问题描述】:
我已经使用这行代码在MkMapView 上添加了超过 2000 个自定义注释。然后首先收到内存警告和应用程序崩溃。
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
//MKAnnotationView *annotationView = nil;
else if ([annotation isKindOfClass:[MyAnnotation class]])
{
MKAnnotationView *annotationView = nil;
static NSString *identifier = @"identifier";
annotationView = (MKAnnotationView *)[mapViewForMapScreen dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView) {
annotationView.annotation = annotation;
} else {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.canShowCallout = YES;
}
MyAnnotation *anno=(MyAnnotation*)annotation;
tagForAnnotation=(int)anno.tag;
if (arrayForLatLong.count!=0)
{
if ([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"INSTAGRAM"])
{
annotationView.image=[UIImage imageNamed:@"instagram.png"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
else if([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"TWITTER"])
{
annotationView.image=[UIImage imageNamed:@"twt_pin.png"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
else if ([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"YOUTUBE"])
{
annotationView.image=[UIImage imageNamed:@"youtube.png"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
else if ([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"MEETUP"])
{
annotationView.image=[UIImage imageNamed:@"meetup_iphone"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
else if ([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"FLICKR"])
{
annotationView.image=[UIImage imageNamed:@"flikr.png"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
else if ([[[arrayForLatLong valueForKey:@"sourceType"] objectAtIndex:tagForAnnotation] isEqualToString:@"VK"])
{
annotationView.image=[UIImage imageNamed:@"vk1"];
annotationView.frame=CGRectMake(0, 0, 16, 22);
}
}
infoButton=[[AsyncImageView alloc]init];
if ([[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"small"] isEqualToString:@""]){
if ([[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"medium"] isEqualToString:@""]){
if ([[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"large"] isEqualToString:@""]) {
[infoButton setImage:[UIImage imageNamed:@"profile"] forState:UIControlStateNormal];
}else{
[infoButton setImageURL:[NSURL URLWithString:[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"large"]]];
}
}else{
[infoButton setImageURL:[NSURL URLWithString:[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"medium"]]];
}
}else{
[infoButton setImageURL:[NSURL URLWithString:[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"]valueForKey:@"profilePic"] valueForKey:@"small"]]];
}
[infoButton setFrame:CGRectMake(20,10,30,30)];
infoButton.layer.cornerRadius=infoButton.frame.size.height/2.0;
infoButton.layer.masksToBounds = YES;
[infoButton addTarget:self action:@selector(actionForProfileImage:) forControlEvents:UIControlEventTouchUpInside];
infoButton.layer.borderWidth=1.0;
infoButton.layer.borderColor=[UIColor grayColor].CGColor;
infoButton.tag=tagForAnnotation;
//Followed Button:----
AsyncImageView *followedBtn=[[AsyncImageView alloc]init];
[followedBtn setFrame:CGRectMake(2,8,17,17)];
followedBtn.tag=tagForAnnotation;
if ([[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"isFollowed"] boolValue]==0 ){
[followedBtn setBackgroundImage:[UIImage imageNamed:@"star_grey.png"] forState:UIControlStateNormal];
}else{
[followedBtn setBackgroundImage:[UIImage imageNamed:@"star_blue.png"] forState:UIControlStateNormal];
}
[followedBtn addTarget:self action:@selector(actionForStarImage:) forControlEvents:UIControlEventTouchUpInside];
UIView*viewForCallOut=[[UIView alloc] initWithFrame:CGRectMake(0, 0,50, 50)];
[viewForCallOut addSubview:infoButton];
//[viewForCallOut addSubview:followedBtn];
annotationView.leftCalloutAccessoryView=viewForCallOut;
annotationView.canShowCallout = YES;
return annotationView;
}
return nil;
}
如果我犯了任何错误,请检查我的代码,然后请告诉我。 提前致谢
【问题讨论】:
-
您收到内存警告,并因此崩溃:iDevice 的资源不是无限的。为了“修复”问题,检查是否没有内存泄漏(使用 Instruments/Memory Leak),您可能希望避免使用 2000 个自定义注释(它们都有用吗?都是可见的?)
-
@Larme 是的,它们都很有用,因为我还在自定义调用视图上为所有具有不同不同信息的自定义注释创建事件。
-
您只能加载根据地图区域可见的符号。添加符号作为地图缩放。
-
@NewStackUser 我已经将地图缩放 5 英里,然后我需要一次显示 5 英里的所有注释。我有很多数据要显示。在这种情况下我应该怎么做?跨度>
标签: ios objective-c mkmapview mapkit mkannotation