我有一個自定義UIView
調用ActivityDetailView
,我實例化,然後添加到父視圖控制器內的滾動視圖。分配此自定義視圖時,每增加一次內存就會佔用大約1mb的時間,並且Instruments顯示內存永遠不會被釋放,即使視圖和父視圖控制器各自都調用了它們的dealloc
方法。我收到內存警告,應用程序最終被殺死,所以我顯然做錯了什麼。內存不被釋放MKMapView w/ARC
更新W繞地圖視圖是事業/信息,但我需要一個修復
在自定義ActivityDetailView
筆尖文件,則存在被放大和周圍的用戶的位置爲中心的地圖視圖。當我從筆尖移除該地圖視圖時,它不會在屏幕上繪圖,內存分配問題就會消失。不過,我顯然需要地圖視圖。爲什麼當地圖視圖超出範圍時地圖視圖的數據不會被釋放?
當視圖顯示時,只有1 ActivityDetailView
和1 ActivityDetailViewController
存活。只要我將視圖從堆棧中彈出,它們就不再存在。即使通過儀器顯示對象被殺死,內存的持續增長也沒有意義。如果父視圖解除分配,爲什麼地圖視圖數據未被解除分配?
我在做什麼錯,或者我應該檢查什麼?
這裏是自定義視圖:
@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];
}
下面是從儀器顯示,大多數的存儲器是從筆尖加載的圖像
我沒有任何東西g暫時緩存。該視圖基本上是一個滾動視圖,其中包含一個mapview,一些標籤和一些表視圖。表視圖沒有填充任何東西,我不在視圖內加載任何圖像,除了滾動視圖的背景圖像以外,但是當視圖發生時,它應該被釋放。
不知道這是否是問題(也不應該是)。但是這兩行代碼是錯誤的detailView = nil; [detailView removeFromSuperview] ;.您應該調用removeFromSuperView,然後將其設置爲nil。 – Cosmin 2013-05-07 13:23:17
@Cosmin我認爲沒關係,如果它仍然被添加到另一個視圖中,它不會達到dealloc ...我猜我們的OP只是在嘗試一些東西,並將它留下。 – 2013-05-07 13:27:40
@Cosmin As Grady說,我只是在嘗試任何事情,看看它是否有所作爲,但事實並非如此。 – ZeNewb 2013-05-07 13:31:30