uiActivityindicatorView in splashscreen creates memory leakage problem? - iphone

I want to display UIActivityIndicatorView over splash screen.
I'm just creating a splashView and activityindicator over splashview in AppDelegate.
- (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];
}
It is working fine except memory leakage.
I'm getting message in console that autoreleased with no pool in place - just leaking.
What i m doing wrong?
Any help will be appreciated.

You don't seem to be releasing anything here.

Your thread method getInitialData must create and drain an autorelease pool. This is done automatically for the main thread but not for any extra threads that you create. Just add this at the top of the method:
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
and this at the bottom:
[localpool drain];
The reason that you are getting the error message is because viewController.view is returning an autoreleased object and you don't have an autorelease pool in place on the thread.

There's a couple problems here. You need to release anything you alloc. The variables splashview, splashImageView, and progressIndicator are allocated but not released, so those will leak.
The message you are getting about the NSAutoreleasePool is because you are doing getInitialData: on a separate thread. NSAutoreleasePools are per-thread, so you you need to do this:
-(void)getInitialData:(id)obj {
NSAutoreleasePool pool = [NSAutoreleasePool new];
[NSThread sleepForTimeInterval:3.0];
[splashView removeFromSuperview];
[window addSubview:viewController.view];
[pool release];
}

Do you really need to make as ivar/properties ?
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
The following line is not none of my business:
I do not know, but first time i am seeing that some one is adding the
activity indicator on splash image. and why do you need to have
splashImageView, you could have directly make an entry in your plist
file for LaunchImage key entry

Related

I am getting 2 different images in place of splash screen

I am getting an different image at the start of the app like a splash screen then i am getting my actual image that i placed in coding.
I placed the splash screen with the following code in my didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.tabBarController;
self.tabBarController.delegate=self;
[window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
LoginViewController *vc = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
self.loginNav = [[UINavigationController alloc] initWithRootViewController:vc];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if([userDefaults valueForKey:#"UserName"] &&[userDefaults valueForKey:#"Password"])
{
vc.username=[userDefaults valueForKey:#"UserName"];
vc.password=[userDefaults valueForKey:#"Password"];
vc.autoLogin=YES;
[vc loginSelectorMethod];
}
else
{
[self.window addSubview:self.loginNav.view];
[self.window makeKeyAndVisible];
}
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashView.image = [UIImage imageNamed:#"splashscreen.png"];
[window addSubview:splashView];
[window bringSubviewToFront:splashView];
[self performSelector:#selector(removeSplash) withObject:nil afterDelay:3.0];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
NSLog(#"Registering for remote notifications");
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
Before the splash screen appears , the "Arrow.png" image appears then my splash screen appers.
If i delete the "Arrow.png" then in place of that image another images appears i.e., "aboutus.png" like that it continues.
I searched in my project for the "Arrow.png" it only appears once in my whole project in the coding.
here you add subiview as a tabbar like bellow..
[window addSubview:tabBarController.view];
after you add loginview like bellow..
[self.window addSubview:self.loginNav.view];
and after that you add splashscreen like bellow..
splashView.image = [UIImage imageNamed:#"splashscreen.png"];
[window addSubview:splashView];
So this is the problem that you seen more then screen instead of splashscreen.
use bellow code...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashView.image = [UIImage imageNamed:#"splashscreen.png"];
[window addSubview:splashView];
[window bringSubviewToFront:splashView];
[self performSelector:#selector(removeSplash) withObject:nil afterDelay:3.0];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
NSLog(#"Registering for remote notifications");
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
and in removeSplash method add this view as a subview of window like bellow..
-(void)removeSplash{
[splashView removeFromSuperView];
LoginViewController *vc = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
self.loginNav = [[UINavigationController alloc] initWithRootViewController:vc];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if([userDefaults valueForKey:#"UserName"] &&[userDefaults valueForKey:#"Password"])
{
vc.username=[userDefaults valueForKey:#"UserName"];
vc.password=[userDefaults valueForKey:#"Password"];
vc.autoLogin=YES;
[vc loginSelectorMethod];
}
else
{
[self.window addSubview:self.loginNav.view];
[self.window makeKeyAndVisible];
}
}
Have you set any Launch image in ur project setting or have you put any image named "Default.png" in your project bundle this kind of image get detect by OS automatically while launch our app please check this 2 points.
edit:-
Than the problem is conflict in TabBar & LoginView & splaceImage.
For this do below stuff I thing this resolve your double image issue.
First, put below code in your DidFinishLaunching() method
//Delay second that how much time u show your splace image
int64_t delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//Do need Full like Add Login View or Add TabbBar
//Remove SplaceImageView From Window
});
splashView = [[UIImageView alloc] initWithFrame:self.window.frame];
splashView.image = [UIImage imageNamed:#"Default-Portrait~ipad.png"];
[self.window addSubview:splashView];
[self.window bringSubviewToFront:splashView];
return YES;
and also one more thing add default splace image like
for iPhone Portrait Default.png.
for iPad, Portrait Default-Portrait~ipad.png
follow as apple document for default image and then check.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.sql_ = [SQLDb initEngine];
[self setupControllers]; /// Set up Yor ToolBar Controller
self.hvController = [[HomeViewController alloc] init];
self.window.rootViewController = self.hvController;
[self.window makeKeyAndVisible];
[self setupSplash];
return YES;
}
-(void) setupSplash
{
self.imvSplash = [[UIImageView alloc] initWithFrame:self.window.bounds];
if( IS_IPHONE_5 )
[self.imvSplash setImage: [UIImage imageNamed:#"Default-568h#2x.png"]];
else
[self.imvSplash setImage: [UIImage imageNamed:#"splash.png"]];
[self.window addSubview: self.imvSplash];
[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:#selector(hideSplash:) userInfo:nil repeats:NO];
}
- (void)hideSplash:(NSTimer *)theTimer
{
[UIView animateWithDuration:1.0
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^{
self.imvSplash.alpha = 0.0;
self.ngNavigationController.view.alpha = 1.0;
}
completion:^(BOOL finished)
{
//[self.ngController setupImageAction];
[self.imvSplash removeFromSuperview];
}];
}

How to move to another view without using a button

My first view displays an image and action indicator while web-servers and database function are run in that background. I want the application to go to my tab view when the functions have been completed. How do I do this?
Here is what the views look like.
What I have tried:
TabBarViewController *tab = [[TabBarViewController alloc]init];
[self presentViewController:tab animated:NO completion:nil];
and
[self performSegueWithIdentifier:#"first" sender:self];
Please can you help my to understand how to do this. I have spent many hours googling this problem and couldn't work out how to do it.
Thanks
EDIT: Added Code
Since you are downloading data you can get a call when the downloading request gets finished
like the method below
- (void)requestFinished:(ASIHTTPRequest *)request
and in this method you can very well present your TabBarViewController.
You can use GCD to make this happen.
For instance in your firstViewController where you trigger downloading you can do:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[model downloadData];
dispatch_sync(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:#"YourSegueIdentifier" sender:self];
});
});
I assume your downloadData method is synchronous, if not you can use notifications in your model. Once data is retrieved you could postNamedNotification from NSNotificationCenter and in firstViewController you could register for notification and after receiving it you would call performSegueWithIdentifier
You Can Declare A separate Method for that.
Call the below method when your function completes.
[self performSelector:#selector(gotonext:) withObject:nil afterDelay:4.0];
-(void)gotonext;
{
TabBarViewController *tab = [[TabBarViewController alloc]init];
[self presentViewController:tab animated:NO completion:nil];
}
Take one splashView Image at starting, like bellow...
#interface AppDelegate : UIResponder <UIApplicationDelegate>{
UIImageView *splashView;
}
#property (nonatomic, retain) UIImageView *splashView;
#end
in AppDelegate.m file...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
splashView = [[UIImageView alloc] initWithFrame:iphoneFrame];
splashView.image = [UIImage imageNamed:#"Default"];
[self.window addSubview:splashView];
[self.window makeKeyAndVisible];
UIViewController *viewController1 = [[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil] autorelease];
UINavigationController *navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navviewController1.title = #"FirstTitle";
// navviewController1.navigationBarHidden=YES;
UIViewController *viewController2 = [[[yourviewController2 alloc] initWithNibName:#"yourviewController2" bundle:nil] autorelease];
UINavigationController *navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
// navviewController2.navigationBarHidden=YES;
navviewController2.title = #"SecondTitle";
UIViewController *viewController3 = [[[yourviewController3 alloc] initWithNibName:#"yourviewController2" bundle:nil] autorelease];
UINavigationController *navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
// navviewController3.navigationBarHidden=YES;
navviewController3.title = #"ThirdTitle";
//..... and so on depend on your requirement
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2 , navviewController3 ,nil];
[self performSelector:#selector(loadViewIphone) withObject:nil afterDelay:2.0];//**add this line at your all data are loading completed**
}
else
{
splashView = [[UIImageView alloc] initWithFrame:ipadFrame];
splashView.image = [UIImage imageNamed:#"Default_iPad"];
[self.window addSubview:splashView];
[self.window makeKeyAndVisible];
[self performSelector:#selector(loadViewIpad) withObject:nil afterDelay:2.0];
}
[self.window makeKeyAndVisible];
return YES;
}
-(void)loadViewIphone
{
[splashView removeFromSuperview];
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionFromBottom];
[animation setDuration:1.0];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[[self.window layer] addAnimation:animation forKey:#"transitionViewAnimation"];
[self.window makeKeyAndVisible];
}
i hope this help you...
Hm... What if you put
TabBarViewController *tab = [[TabBarViewController alloc]init];
[self presentViewController:tab animated:NO completion:nil];
In
dispatch_async(dispatch_get_main_queue(), ^{
//stop the loader once the database stuff has finished and get rid of the text
[[self firstLoader]stopAnimating];
self.downloadingLabel.text = #"";
});
UPDATE: If you want to do this sync
dispatch_sync(a_queue, ^{ wait_for_me(); });
And after that present your VC.

how can i show 2 intro views when the app is launching?

I know if there is a file named Default.png it shows the image automatically.
but, I want to change the image during it showes.
FYI, I have 2 images Default.png and default2.png.
I want to display default2.png after show Default.png.
I tried followed codes, but it didn't work.
What do I have to do?
-(BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//..................................................................
//self.window bullabulla~
[self performSelectorOnMainThread:#selector(showIntro2View) withObject:nil waitUntilDone:YES];
//[self showIntro2View]; //also tried this, but not work.
[self.window addsubview:tabbarController];
[self.window makeKeyAndVisible];
}
-(void) showIntro2View {
UIImageView *intro2 = [[UIImageView alloc]initwithframe:cgrectmake(0,0,320,460)];
intro2.image = [UIImage imagenamed:#"default2.png"];
[self.window addSubview:intro2];
[self.window bringSubviewToFront:intro2];
[NSThread sleepfortimeinterval:2];
}
In your APP DELEGATE.
Something like this should do the job:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.window.bounds] autorelease];
UIImage *image = [UIImage imageNamed:#"NAMEOFYOURSPLASHSCREEN.png"];
imageView.image = image;
[self.window addSubview:imageView];
[self.window makeKeyAndVisible];
[self performSelector:#selector(remove1stSplash:) withObject:imageView afterDelay:5];
return YES;
}
- (void)remove1stSplash:(UIImageView *)1stView {
UIImageView *imageView = ...
[self.window addSubview:imageView];
[self performSelector:#selector(remove2ndSplash:) withObject:imageView afterDelay:5];
[1stView removeFromSuperView];
}
- (void)remove2ndSplash:(UIImageView *)2ndView {
[self.window addSubview:.....
[2ndView removeFromSuperView];
}
EDIT:
Link for a sample project:
Two Splash Screen display in iphone
As you may know, you don't have real influence on how long the Default img is displayed. But you know when it shows and hides. It shows only, when the app runs the first time, the app is not in background or has been terminated after a while of not using it.
The first View Controller that is called, should implement viewWillAppear, as it is something like the last method called before the default image hides. There everything is loaded and you can immediately add a subview to the main window.
I'd do it in 2 different methods like showOverrideDefaultImage and hideOverrideDefaultImage. In the show method you add your own View to the appDelegate window and fire a
[self performSelector:#selector(hideOverrideDefaultImage) withObject:nil afterDelay:2];
In the hide method simply remove your view from superview. Therefore you have to have a valid pointer to this object.
that's it. Don't use NSThread.
Do this:
-(void) showIntro2View
{
UIImageView *intro2 = [[UIImageView alloc]initwithframe:cgrectmake(0,0,320,460)];
intro2.image = [UIImage imagenamed:#"default2.png"];
intro2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Default.png"],
[UIImage imageNamed:#"default2.png"], nil];
intro2.animationDuration = 2.0f;
intro2.animationRepeatCount = 0;
[intro2 startAnimating];
[self.window addSubview:intro2];
[self.window bringSubviewToFront:intro2];
[NSThread sleepfortimeinterval:2];
}

Utilizing a UIActivityIndicatorView and CorePlot-CocoaTouch causes memory prblem

Utilising CorePlot-Cocoa Touch in an IOS5.1 app. Since the setting up of the scatterplots, dataForPlots, legends etc takes time, decided to include a UIActivityIndicatorView to indicate something is happening to the user.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
progressInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
progressInd.center = self.view.center;
[progressInd sizeToFit];
progressInd.hidden = NO;
progressInd.alpha = 1.0;
[self.view addSubview:progressInd];
[progressInd startAnimating];
[self.view bringSubviewToFront:progressInd];
[self performSelectorInBackground:#selector(DoMakeupPlot) withObject:nil];
}
- (void)DoMakeupPlot
{
… set up plot
… including datasource and delegate
[progressInd stopAnimating];
[progressInd removeFromSuperview];
progressInd = nil;
}
Now this seems to work on occasion, however it's apparent that there becomes 2 Threads calling the plot datasource routines:
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
which does on occasion cause memory problems and the app crashes.
Experimented exiting these 2 Threads from within the 2 above routines, but this causes other problems.
Now prior to using this UIActivityIndicatorView, whereby the 2 routines above are called from the MainThread, all is good.
Also tried this with the MBProgressHud, and obviously have the same problem.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Loading";
HUD.detailsLabelText = #"plot data";
HUD.square = YES;
[HUD showWhileExecuting:#selector(DoMakeupPlot) onTarget:self withObject:nil animated:YES];
}
How do I remove the Thread which performs the number crunching, whilst the MainThread does the UIActivityIndicatorView, after it has done the setting up?
Help appreciated.
Don't set the datasource until -DoMakeupPlot finishes. That way the plot doesn't try to load data from the datasource that's not ready yet.

problem i loading progress indicator in iphone sdk?

I want to get list from server it is taking some time So at that time I'm displaying progress indicator. My problem sometimes the progress indicator is appearing and some times it is not appearing. The code I used is
-(void)viewWillAppear:(BOOL)animated
{
[self loadprogressIndicator];
}
-(void)loadprogressIndicator
{
MessengerAppDelegate* appDelegate = (MessengerAppDelegate*) [ [UIApplication sharedApplication] delegate];
[appDelegate showWaitingView];
[NSThread detachNewThreadSelector:#selector(statusIndicatorForRefresh) toTarget:self withObject:nil];
}
-(void)statusIndicatorForRefresh
{
NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self performSelectorOnMainThread:#selector(loadList) withObject:nil waitUntilDone:NO];
[NSThread exit];
[pool release];
}
-(void)loadList
{
MessengerAppDelegate* appDelegate = (MessengerAppDelegate*) [ [UIApplication sharedApplication] delegate];
customerList = [appDelegate getCustomersArray];
[theTableView reloadData];
[appDelegate removeWaitingView];
}
And in appdelegate.m I implemeted these two methods
- (void)showWaitingView
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGRect frame = CGRectMake(90, 190, 32, 32);
UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[progressInd startAnimating];
progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
frame = CGRectMake(130, 193, 140, 30);
UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
waitingLable.text = #"Processing...";
waitingLable.textColor = [UIColor whiteColor];
waitingLable.font = [UIFont systemFontOfSize:20];
waitingLable.backgroundColor = [UIColor clearColor];
frame = [[UIScreen mainScreen] applicationFrame];
UIView *theView = [[UIView alloc] initWithFrame:frame];
theView.backgroundColor = [UIColor blackColor];
theView.alpha = 0.7;
theView.tag = 999;
[theView addSubview:progressInd];
[theView addSubview:waitingLable];
[progressInd release];
[waitingLable release];
[window addSubview:[theView autorelease]];
[window bringSubviewToFront:theView];
[pool drain];
}
- (void)removeWaitingView
{
UIView *v = [window viewWithTag:999];
if(v) [v removeFromSuperview];
}
Can anyone please help me. Happy Coding..
Thanks in advance.
Praveena..
You are doing all your activity on the main thread. Your app will appear to be blocked because you block the UI thread.
Also what is the use of launching an NSThread if all it does is call a method on your main thread and not even wait for it to complete? You are doing no multithreading at all.
Your app does this:
in main thread:
show waiting view
do 1 run loop cycle
do your data processing
hide waiting view
since your data processing is in the main thread you block everything there. You should do the processing in a secondary thread and leave the main thread for UI handling.