我有一个自定义UIView
调用ActivityDetailView
,我实例化然后添加到父视图控制器中的滚动视图。当这个自定义视图被分配时,每次额外的内存占用大约 1mb 并且 Instruments 显示内存永远不会被释放,即使视图和父视图控制器都dealloc
调用了它们的方法。我收到内存警告,应用程序最终被杀死,所以我显然做错了什么。
更新了有关地图视图的信息是原因,但我需要修复
在自定义ActivityDetailView
nib 文件中,有一个以用户位置为中心的地图视图。当我从笔尖移除此地图视图以使其不在屏幕上绘制时,内存分配问题就消失了。但是,我显然需要地图视图。为什么地图视图超出范围时不会发布地图视图的数据?
视图显示时只有 1ActivityDetailView
和 1ActivityDetailViewController
活着。一旦我从堆栈中弹出视图,它们就不再存在了。即使通过 Instruments 显示对象被杀死,内存如何保持增长也没有意义。如果父视图被释放,为什么地图视图数据没有被释放?
我做错了什么或者我应该检查什么?
这是自定义视图:
@interface ActivityDetailView ()
{
CLLocation *location;
__weak id parentViewController;
int scrollViewX;
ImageUtility *imageUtility;
}
@end
@implementation ActivityDetailView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
NSArray *xibViews = [[NSBundle mainBundle] loadNibNamed:@"ActivityDetailView" owner:nil options:nil];
if ([xibViews count] < 1) return nil;
ActivityDetailView * xibView = [xibViews objectAtIndex:0];
[xibView setFrame:frame];
self = xibView;
}
return self;
}
- (id)initWithLocation:(CLLocation *)loc parentController:(id)parent
{
self = [self initWithFrame:CGRectMake(0, 0, 320, 1000)];
if (self)
{
imageUtility = [ImageUtility sharedManager];
location = loc;
parentViewController = parent;
scrollViewX = 0;
[self centerMapForActivityLocation];
[self addPhotoButtonWithImageNamed:@"addActivityPhoto.png" target:parentViewController selector:@selector(addPhotoToActivity:)];
}
return self;
}
- (void)dealloc
{
NSLog(@"ActivityDetailView was dealloced");
}
在父视图控制器中:
@interface ActivityDetailViewController ()
{
ActivityDetailView *detailView;
}
@end
@implementation ActivityDetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Some code left out for clarity
[self setupView];
}
- (void)didReceiveMemoryWarning
{
NSLog(@"Purging image cache");
[[ImageUtility sharedManager] purgeCache];
}
- (void)dealloc
{
// These essentially do nothing to help the problem
detailView.mapView = nil;
[detailView removeFromSuperview];
detailView = nil;
self.scrollView = nil;
NSLog(@"ActivityDetailViewController was dealloced");
}
- (void)setupView
{
// Add the activity detail view to the scroll view
detailView = [[ActivityDetailView alloc] initWithLocation:self.activityLocation parentController:self];
[self.scrollView addSubview:detailView];
self.scrollView.contentSize = detailView.frame.size;
// Setup the map view
detailView.mapView.delegate = self;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = self.activityLocation.coordinate;
[detailView.mapView addAnnotation:annotation];
if (self.activity.mapImageName) {
detailView.mapView.scrollEnabled = YES;
detailView.mapView.zoomEnabled = YES;
} else {
detailView.mapView.scrollEnabled = NO;
detailView.mapView.zoomEnabled = NO;
}
// Add the weather area to the view
dayView = [[DailyButtonView alloc] initWithFrame:CGRectMake(0, -17, 60, 70)];
dayView.hidden = YES;
[detailView.weatherView addSubview:dayView];
}
这是 Instruments 的一张图片,显示大部分内存来自 nib 加载
我目前没有要缓存的东西。该视图基本上是一个滚动视图,其中包含一个地图视图以及一些标签和几个表格视图。表格视图没有填充任何内容,并且除了滚动视图的背景图像之外,我没有在视图中加载任何图像,但是当视图加载时应该会被释放。