2016-09-13 20 views
1

我已經使用這行代碼添加多於自定義註釋MkMapView。然後首先收到內存警告和應用程序崩潰。添加自定義註釋,然後應用程序崩潰 - ios

-(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; 
} 

請檢查我的代碼,如果我做了任何錯誤,請讓我知道。 在此先感謝

+1

你得到了一個內存警告,並因此而崩潰:一個iDevice的資源並不是無限的。爲了「解決」這個問題,檢查是否沒有內存泄漏(帶有儀器/內存泄漏),並且您可能想要避免使用2000個自定義註釋(它們都是有用的嗎?所有可見的?) – Larme

+0

@Larme是的他們都是有用的,因爲我也創建了自定義調用視圖上的事件與所有自定義註釋與不同的不同信息。 –

+0

您只能加載那些根據地圖區域可見的符號。添加符號作爲地圖縮放。 – NewStackUser

回答

0

當您有大量的數據點導致大量的註釋時,一個好方法是聚集數據點併爲每個羣集添加一個註釋。在地圖放大到一個集羣中時,用集羣中數據點的註釋替換表示集羣的註釋。在縮小集羣時,可以用代表集羣的註釋替換表示數據點的註釋。這種方法減少了註釋的總數,並使數據更易於理解。 This SO questionthis post會給你更多關於聚類的信息。