2011-09-28 17 views
0

我想在啓動畫面上顯示UIActivityIndi​​catorView。 我只是在AppDelegate上通過splashview創建splashView和activityindicator。在splashscreen中的uiActivityindicatorView創建內存泄漏問題?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    //[NSThread sleepForTimeInterval:3]; 

    // Override point for customization after application launch. 

    // Add the view controller's view to the window and display. 

    splashView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 

    UIImage *splashImage = [UIImage imageNamed:@"Splashimage.png"]; 
    splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 
    splashImageView.image = splashImage; 
    [splashView addSubview:splashImageView]; 

    progressIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(145,440,30,30)]; 
    progressIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; 
    [progressIndicator startAnimating]; 
    [splashView addSubview:progressIndicator]; 

    [self.window addSubview:splashView]; 
    [NSThread detachNewThreadSelector:@selector(getInitialData:) 
         toTarget:self withObject:nil]; 


    [self.window makeKeyAndVisible]; 
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone]; 

    return YES; 
} 

- (void)getInitialData:(id)obj { 
    [NSThread sleepForTimeInterval:3.0]; 
    [splashView removeFromSuperview]; 
    [window addSubview:viewController.view]; 
} 

它工作正常,除了內存泄漏。 我在控制檯中收到消息,自動釋放沒有到位的地方 - 只是漏水。 我做錯了什麼? 任何幫助將不勝感激。

回答

0

你似乎沒有在這裏發佈任何東西。

0

你真的需要作爲伊娃/屬性嗎?

UIActivityIndicatorView* progressIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(145,440,30,30)]; 
progressIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; 
[progressIndicator startAnimating]; 
[splashView addSubview:progressIndicator]; 
[progressIndicator release];    // release 
[self.window addSubview:splashView]; 
[splashView release];  // release 

以下行不關我的事:

我不知道,但我第一次看到,一些之一上添加飛濺圖像 活動的指標。爲什麼你需要有 splashImageView,你可以有直接讓你的plist 文件LaunchImage鍵進入

+0

因爲我M狀加載從網頁圖像的UITableView,我想閃屏上顯示indicatorview因此將clearify的東西被加載。 –

+0

ahhh,這是非常討厭的方式,所以如果你要在你的tableview中加載圖像,那麼你爲什麼要在應用程序啓動時顯示活動指示器。用戶可能會感到困惑,該應用程序沒有啓動或出現問題。你應該在tableview上顯示活動指示器。如果數據相同,您可以使用緩存。 –

+0

非常感謝Praveen-K的建議。 –

0

你的線程方法getInitialData必須創建和漏自動釋放池的條目。這是針對主線程自動完成的,但不針對您創建的任何額外線程。

NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init]; 

在底部,這樣的::就在該方法的頂部添加這個,因爲viewController.view將返回一個autoreleased對象

[localpool drain]; 

你得到該錯誤消息的原因和您沒有在線程上使用自動釋放池。

+0

羅賓謝謝。我犯了同樣的錯誤。 –

0

這裏有幾個問題。你需要釋放你分配的任何東西。變量splashview,splashImageViewprogressIndicator已分配但未發佈,因此這些變量將會泄漏。

您收到的有關NSAutoreleasePool的消息是因爲您在單獨的線程上執行getInitialData:NSAutoreleasePool s爲每線程,所以你需要做到這一點:

-(void)getInitialData:(id)obj { 
    NSAutoreleasePool pool = [NSAutoreleasePool new]; 
    [NSThread sleepForTimeInterval:3.0]; 
    [splashView removeFromSuperview]; 
    [window addSubview:viewController.view]; 
    [pool release]; 
} 
+0

非常感謝lcydog。它對我來說是一種魅力:-) –