Memory management 管理映射iOS上的内存分配问题
在我的iPhone应用程序中,我有一个地图视图。在本文中,我将根据web服务器的数据显示多个pin视图。这是我使用的方法Memory management 管理映射iOS上的内存分配问题,memory-management,ios6,annotations,mkmapview,Memory Management,Ios6,Annotations,Mkmapview,在我的iPhone应用程序中,我有一个地图视图。在本文中,我将根据web服务器的数据显示多个pin视图。这是我使用的方法 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { NSString *identifier = @"myPin"; self.pinView = (MKPinAnnotationView *)[se
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSString *identifier = @"myPin";
self.pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (self.pinView == nil) {
self.pinView= [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]autorelease]; //11.1%
} else {
self.pinView.annotation = annotation;
}
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; //20.4%
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
self.pinView.rightCalloutAccessoryView = rightButton; //2.7%
MyAnnotation *annot = (MyAnnotation*)annotation;
UIImageView *egoimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"defaultPerson"]]; //17.5%
NSString *imageUrl = [NSString stringWithFormat:@"%@%@", CommonImageURL, [friendsProfileImageArray objectAtIndex:annot.tag]]; //9.4%
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
([UIScreen mainScreen].scale == 2.0)) {
// Retina display
[egoimageView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"defaultPerson@2x.png"]]; //2.6%
egoimageView.image = [UIImage imageWithCGImage:egoimageView.image.CGImage scale:egoimageView.image.size.width/25 orientation:egoimageView.image.imageOrientation];
} else {
// non-Retina display
[egoimageView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]]; //14.6%
egoimageView.image = [UIImage imageWithCGImage:egoimageView.image.CGImage scale:egoimageView.image.size.width/25 orientation:egoimageView.image.imageOrientation]; //1.6%
}
[egoimageView sizeToFit];
self.pinView.leftCalloutAccessoryView = egoimageView; //3.1%
[egoimageView release];
self.pinView.canShowCallout=YES;
self.pinView.animatesDrop=YES;
//pin color based on status.....
if ([annot.relationshipStatus intValue]==2 )
self.pinView.pinColor=MKPinAnnotationColorGreen;
else
self.pinView.pinColor=MKPinAnnotationColorPurple; //17.1%
return self.pinView;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
NSString*标识符=@“myPin”;
self.pinView=(MKPinAnnotationView*)[self.mapView出列可重用AnnotationViewWithIdentifier:identifier];
if(self.pinView==nil){
self.pinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:标识符]自动释放];//11.1%
}否则{
self.pinView.annotation=注释;
}
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailDisclosure];//20.4%
[rightButton setTitle:annotation.title for状态:UIControlStateNormal];
self.pinView.rightCalloutAccessoryView=rightButton;//2.7%
MyAnnotation*Annott=(MyAnnotation*)注释;
UIImageView*EGOMageView=[[UIImageView alloc]initWithImage:[UIImageName:@“defaultPerson”]];//17.5%
NSString*imageUrl=[NSString stringWithFormat:@“%@%@”,CommonImageURL[friendsProfileImageArray对象索引:annot.tag]];//9.4%
如果([[UIScreen Main Screen]响应选择器:@selector(displayLinkWithTarget:selector:)]&&
([UIScreen mainScreen]。比例==2.0)){
//视网膜显示器
[egoimageView setImageWithURL:[NSURL URLWithString:imageUrl]占位符图像:[UIImage ImageName:@]defaultPerson@2x.png"]]; //2.6%
egoimageView.image=[UIImage imageWithCGImage:egoimageView.image.CGImage比例:egoimageView.image.size.width/25方向:egoimageView.image.imageOrientation];
}否则{
//非视网膜显示
[egoimageView setImageWithURL:[NSURL URLWithString:imageUrl]占位符图像:[UIImage ImageName:@“defaultPerson.png”]];//14.6%
egoimageView.image=[UIImage imageWithCGImage:egoimageView.image.CGImage比例:egoimageView.image.size.width/25方向:egoimageView.image.imageOrientation];//1.6%
}
[egoimageView sizeToFit];
self.pinView.leftCalloutAccessoryView=egoimageView;//3.1%
[电子图像视图发布];
self.pinView.canShowCallout=YES;
self.pinView.animatesDrop=YES;
//基于状态的pin颜色。。。。。
如果([annot.relationshipStatus intValue]==2)
self.pinView.pinColor=MKPinAnnotationColorGreen;
其他的
self.pinView.pinColor=MKPinAnnotationColorPurple;//17.1%
返回self.pinView;
}
在本文中,我提到了内存分配的百分比。如果我持续加载地图,总内存使用量会增加,应用程序会崩溃。我怎样才能正确地解决这个问题?我已经试着把它修好了,但我不知道还能做些什么
请提供帮助,提前感谢。为什么要将注释视图声明为属性?这看起来不对。如果只有一个注释,但没有多个注释,则该方法可能有效。在viewForAnnotation方法本身中将pinView声明为局部变量。我按照您所说的修改了代码,并将其转换为ARC。但是仍然存在问题。当您从数据源(例如,如果某些NSArray)和地图本身更改区域时,是否卸载管脚?是的,每次都会从地图视图中删除以前的所有注释。