我一直试图摆脱mapview中的内存泄漏。我正在使用自定义地图引脚类。 一切正常,但问题是 - 我需要过滤mapview结果。当我删除所有mapview注释 - 并添加过滤结果 - 性能工具发现泄漏。但在我使用的mapPin类中使用autorelease,因此它们应该被释放,但它们不是。我究竟做错了什么?
MapPin.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKMapView.h>
#import <MapKit/MKAnnotation.h>
@interface MapPin : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString * picture;
NSInteger tag_number;
}
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic num:(NSInteger) number;
- (void) setPic:(NSString *) picture;
- (NSString* ) getPic;
- (void) setNum:(NSInteger) tag_number;
- (NSInteger ) getNum;
@end
MapPin.m
#import "MapPin.h"
@implementation MapPin
@synthesize coordinate = _coordinate;
@synthesize title = _title;
@synthesize subtitle = _subtitle;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord
{
return [self initWithCoordinate:coord title:@""];
}
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title {
return [self initWithCoordinate:coord title:title subtitle:@""];
}
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle {
return [self initWithCoordinate:coord title:title subtitle:subtitle image:@""];}
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic{
MapPin * me = [[[MapPin alloc] init] autorelease];
me.coordinate = coord;
me.title = title;
me.subtitle = subtitle;
[me setPic:pic];
return me;
}
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic num:(NSInteger) number{
MapPin * me = [[[MapPin alloc] init] autorelease];
me.coordinate = coord;
me.title = title;
me.subtitle = subtitle;
[me setPic:pic];
[me setNum:number];
return me;
}
- (void) setPic:(NSString*) pic {
picture = pic;
}
- (NSString * ) getPic{
return picture;
}
- (void) setNum:(NSInteger) number {
tag_number = number;
}
- (NSInteger ) getNum{
return tag_number;
}
@end
答案 0 :(得分:1)
我一直在使用由 Mayur Birari 创建的cutom地图图钉,我稍微调整一下,以支持自定义地图图片图片和ID。
<强> CustomMapPin.h 强>
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface CustomMapPin : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString* title;
NSString* subtitle;
NSString* pic;
NSInteger tag_number;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subtitle;
@property (nonatomic, copy) NSString* pic;
@property (nonatomic) NSInteger tag_number;
@end
<强> CustomMapPin.m 强>
#import "CustomMapPin.h"
@implementation CustomMapPin
@synthesize title;
@synthesize subtitle;
@synthesize coordinate;
@synthesize pic;
@synthesize tag_number;
- (void)dealloc
{
self.title = nil;
self.pic = nil;
self.subtitle = nil;
[super dealloc];
}
@end
并在课堂上使用它:
CLLocationCoordinate2D pinlocation;
循环中的我设置了所需的值并创建了一个地图引脚:
pinlocation.latitude = ...;
pinlocation.longitude = ...;
NSInteger pinID = ....;
CustomMapPin* customMapPin=[[CustomMapPin alloc] init];
customMapPin.coordinate=(CLLocationCoordinate2D
{pinlocation.latitude,pinlocation.longitude};
customMapPin.title=@"title";
customMapPin.subtitle=@"subtitle";
customMapPin.pic = @"customImageName";
customMapPin.tag_number = pinId;
[mapView addAnnotation:customMapPin];
设置自定义图片:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass: [CustomMapPin class]])
{
CustomMapPin * a = annotation;
[annView setImage:[UIImage imageNamed:a.pic]];
}
}
在标注上输入图钉ID:
- (void)mapView:(MKMapView *)mp annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
CustomMapPin * v = (CustomMapPin *) view.annotation;
int tagNumber = v.tag_number;
....
}
最后 - 在我的项目中需要有过滤器按钮 - 所以我需要删除所有引脚,并添加所需的。默认情况下调用mapview删除所有注释创建的内存泄漏。因此,当我需要从注释中清除mapview时,我称之为函数:
- (void)removeAnnotations
{
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:[mapView.annotations count]];
for (id annotation in mapView.annotations)
{
if (annotation != mapView.userLocation)
{
[toRemove addObject:annotation];
}
}
[mapView removeAnnotations:toRemove];
for(int i = 0; i < [toRemove count]; i++)
{
CustomMapPin * a = [toRemove objectAtIndex:i];
[a release];
a = nil;
}
}
希望这会有所帮助 快乐的编码! :)
答案 1 :(得分:0)
你只是缺少dealloc实现!
例如:
- (void)dealloc
{
[self.title release];
self.title = nil;
self.subtitle release];
self.subtitle = nil;
[super dealloc];
}