Why is this ImageView not being released? - ios

i have a splash page that is displayed on app startup that my client wants me to keep visible for x amount of time. Everything is working great except the image is never released?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:#"splash_page" ofType:#"png"];
NSData *imageData = [[NSData alloc] initWithContentsOfFile:fileLocation];
UIImage *launchImage = [[UIImage alloc] initWithData:imageData];
[imageData release], imageData = nil;
UIImageView *launchImageView = [[UIImageView alloc] initWithImage:launchImage];
launchImageView.frame = CGRectMake(0,
0,
[[UIScreen mainScreen] bounds].size.width ,
[[UIScreen mainScreen] bounds].size.height);
launchImageView.tag = 121;
[launchImage release], launchImage = nil;
[self.window addSubview:launchImageView];
[launchImageView release], launchImage = nil;
[self.window makeKeyAndVisible];
[self performSelector:#selector(initApp) withObject:nil afterDelay:kInitDelay];
return YES;
}
- (void)initApp
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
UIImageView *launchImageView = (UIImageView*)[self.window viewWithTag:121];
[launchImageView removeFromSuperview];
RootNavController *navController = [[RootNavController alloc] initRootController];
self.rootNavController = navController;
[self.window addSubview:navController.view];
[navController release], navController = nil;
}
It is my understanding that removeFromSuperview calls release on the view so this should be released however i can still see a Malloc of 524kb in Intruments Object Allocation tool which im sure is the image. Responsible Library = libRIP.A.dylib and the Responsible Caller ripl_Create.
If i comment out the splash page code and launch the NavController directly i dont have that 524kb.
Any ideas?

Why don't you use the Default.png as your splash screen? Renaming your "Splash_page.png" to "Default.png" will automatically show the splash page on load and then just add UIImageView (with Default.png) to the MainWindow and after x time launch your rootViewController.

Related

self.window setRootViewController not working on xcode 6.1.1

I am facing some issues w.r.t XCode 6.1.1 , here is my code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[DBManager getSharedInstance] initWithdbFile];
[[DBManager getSharedInstance] createtableScripts];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
containerViewController =[[CprViewController alloc] init];
[containerViewController Setup];
[self.window setRootViewController:containerViewController.containerViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self pickModel];
return YES;
}
Now the app works fine in older XCode i.e 5.1.1 but while simulating on XCode 6.1.1 the app launches the Launch image and there is no response i.e the execution ends at [self.window setRootViewController:containerViewController.containerViewController]; and it doesnt proceed to execute further lines of code. It shows no error in console. (The app however runs on device having latest OS (i.e iOS 8). Please help.
*Edited Post -- begins here
Thanks for helping out. Here’s the problem that i am facing,
This is in my AppDelegate file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[DBManager getSharedInstance] initWithdbFile];
[[DBManager getSharedInstance] createtableScripts];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //Now Removed!!!
containerViewController =[[CprViewController alloc] init];
[containerViewController Setup];
self.window.rootViewController = containerViewController.containerViewController;
self.window.backgroundColor = [UIColor whiteColor];//Now Removed!!!
[self.window makeKeyAndVisible];//Now Removed!!!
[self pickModel];
return YES;
}
Now i have removed the three lines just as you suggested and now the execution continues
Now this is my code for [containerViewController Setup];
-(void)Setup
{
SideMenuViewController *leftMenuViewController = [SideMenuViewController homeBookViewController];
CprStartPageViewController *accounts=[[CprStartPageViewController alloc] initWithNibName:#"CprStartPageViewController" bundle:nil];
self.navController= [[CprNavigationViewController alloc]
initWithRootViewController:accounts];
self.containerViewController = [MFSideMenuContainerViewController containerWithCenterViewController:self.navController leftMenuViewController:leftMenuViewController
rightMenuViewController:nil];
[leftMenuViewController.passBookTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:0];
}
The above code attaches a side menu (here i am attaching a left side menu) - No error in this code i.e the execution continues.
Now CprLoginViewController is my parentViewController
i.e the parentViewController for the 4 screen that i had mentioned and this is my initWithNibName for the 4 screens
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil parentViewController:(CprLoginViewController*) inVC
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil parentViewController:inVC];
if (self) {
// Custom initialization
}
return self;
}
and the error now occurs in CprLoginViewController where i am using a procedure called showNextPage to navigate between these 4 pages
-(void) showNextPage
{
if (currentPage > numberPages)
return;
CprLoginSubViewController *controller;
switch(currentPage)
{
case 0:
controller = [[Corppersondtl alloc] initWithNibName:#"Corppersondtl" bundle:nil parentViewController:self];
break;
case 1:
controller = [[CprLoginSub1ViewController alloc] initWithNibName:#"CprLoginSub1ViewController" bundle:nil parentViewController:self];
break;
case 2:
controller = [[CprLoginSub2ViewController alloc] initWithNibName:#"CprLoginSub2ViewController" bundle:nil parentViewController:self];
break;
case 3:
controller = [[CprLoginSub3ViewController alloc] initWithNibName:#"CprLoginSub3ViewController" bundle:nil parentViewController:self];
break;
case 4:
controller = [[CprLoginSub4ViewController alloc] initWithNibName:#"CprLoginSub4ViewController" bundle:nil parentViewController:self];
default:
break;
}
CGRect rBounds;
// add the required controller's view to scroll view
if (controller.view.superview == nil)
{
CGRect frame = self.scrollView.frame;
frame.origin.x = CGRectGetWidth(frame) * (currentPage);
frame.origin.y = 0;
controller.view.frame = frame;
[self addChildViewController:controller];
[self.scrollView addSubview:controller.view];
[controller didMoveToParentViewController:self];
// just scroll to appropriate page
rBounds = self.scrollView.bounds;
rBounds.origin.x = CGRectGetWidth(rBounds) * currentPage;
[self.scrollView scrollRectToVisible:rBounds animated:YES];
}
NSLog(#"scrollView is %lf", self.scrollView.frame.origin.y);
viewOriginalbounds = rBounds; //self.scrollView.frame;
currentPage++;
}
In the above code i am attaching a scrollview to scroll the screen upwards when keyboard appears for text inputs.
Now the line if (controller.view.superview == nil) is throwing exc_bad_access error.
Just delete that lines of code
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

Overriding loadView and setting rootViewController programmatically, no Storyboard

I'm trying to create and set a View as the root View programmatically. I'm not using a Storyboard or ARC.
I'm trying to set a UIWebView as my ViewController's root View, which I'm unable to do. When I run the following code, I see a NavigationBar with a blank white screen. I tried setting a regular UIView as the root View and set its background color to blue, which loaded, so the problem seems to be specific to a UIWebView.
I'd really appreciate it if someone could point out what I'm doing wrong. Thanks!
Here is my loadView code:
// in GmailOAuthViewController.m
- (void)loadView {
CGRect frame = [[UIScreen mainScreen] bounds];
UIWebView *webView = [[[UIWebView alloc] initWithFrame:frame] autorelease];
self.view = webView;
}
And here is my application:didFinishLaunchingWithOptions: code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect frame = [[UIScreen mainScreen] bounds];
UIViewController *viewController = [[GmailOAuthViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window = [[UIWindow alloc] initWithFrame:frame];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
you are assigning self.view...
if you assign webView to view everything else is gone
try
[self.view addSubview: webView]
this is code i just tried and it works
CGRect frame = [[UIScreen mainScreen] bounds];
UIViewController *viewController = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window = [[UIWindow alloc] initWithFrame:frame];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
in ViewController.m
-(void)viewDidAppear:(BOOL)animated{
CGRect frame = self.view.bounds;
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame] ;
webView.delegate = self;
self.view = webView;
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"ViewControllerCatalog" ofType:#"pdf"];
if (thePath) {
NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
[(UIWebView *)self.view loadData:pdfData MIMEType:#"application/pdf"
textEncodingName:#"utf-8" baseURL:nil];
}
}
I apologize I told you wrong earlier you do in fact assign to view

iOS UILaunchImageFile: some 4-inch-screen users see black borders

Some of my users seem to consistently see a clipped screen when running my app on their 4-inch devices. It may be limited to iPod Touch 5g users, and seems to occur on iOS6 and iOS7 (I am still investigating). I have what I think is a simple and correct UILaunchImageFile configuration, and it works fine on my iPhone 5 and in all the simulators. Any ideas?
Info.plist:
...
<key>UILaunchImageFile~ipad</key>
<string>UILaunchImage-iPad</string>
<key>UILaunchImageFile~iphone</key>
<string>UILaunchImage</string>
...
App product filesystem:
MyApp.app/
...
Info.plist
UILaunchImage-568h#2x.png (640x1136)
UILaunchImage-iPad.png (768x1024)
UILaunchImage-iPad#2x.png (1536x2048)
UILaunchImage.png (320x480)
UILaunchImage#2x.png (640x960)
...
[EDIT: my startup code, in MyAppDelegate]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
// screen.bounds is the whole display size;
// screen.applicationFrame is smaller when you show the status bar
UIScreen * screen = [UIScreen mainScreen];
CGRect screenBounds = screen.bounds;
CGRect applicationFrame = screen.applicationFrame;
self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
// load main ui
MyUIViewController * main = [[[MyUIViewController alloc] initWithNibName:#"MyUIViewController" bundle:nil] autorelease];
UIView * rootView = [[[UIView alloc] initWithFrame:screenBounds] autorelease];
main.view = rootView;
[main loadIfNeeded];
self.viewController = main;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
...
}

Adding UIImageView to rootViewController

All I am simply trying to do is display an image on the screen as I am just starting out iOS
development. I figured since UIImageView is a subclass of UIView, I would add it in a similar way but I am not having any luck. I understand this is an easy question but any help would be appreciated.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.rootViewController = [UIViewController new];
UIImage* stallion = [UIImage imageNamed:#"stallion1.png"];
UIImageView* iv = [UIImageView alloc];
iv.image = stallion;
[self.window.rootViewController.view addSubview:iv];
[self.window makeKeyAndVisible];
return YES;
}
Try something like this:
App delegate:
#import "RootViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//allocate and initialize the root view controller
RootViewController *rootViewController = [[RootViewController alloc] init];
//set the root view controller
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
Create a subclass of UIViewController called RootViewController
.h
#import <UIKit/UIKit.h>
//set the class RootViewController as a subcalss of UIViewController
#interface RootViewController : UIViewController
#end
.m
//this is one of the life cycle methods of a UIViewController and should already be in the code when the class is created
- (void)viewDidLoad
{
//execute the viewDidLoad method of the superclass (UIViewController)
[super viewDidLoad];
//allocate and initialize the image view
//assign the image view a frame
//x offset from the left = 0.0
//y offset from the top = 0.0
//width = the view controller's view's width (should be the whole screen)
//height = the view controller's view's height(should be the whole screen)
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
[iv setImage:[UIImage imageNamed:#"myImageName.png"]];
//the background will be red if everything is setup correctly, but the image isn't found
[iv setBackgroundColor:[UIColor redColor]];
//add the image view to the view controller's view
[self.view addSubview:iv];
}
//EDIT
It would be very helpful for you if u will read book in which there are information about View Controllers. This is very essential information. I recommend you to read "The Core IOS 6 Cookbook" by Erica Sadum. It really helped me.
You can also just look at the apple documentation http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/
Write something like this in AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MyViewController *viewController = [[MyViewController alloc] init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
Next you must add the UIImageView but only when the view of the created view controller is loaded. The best method for this is viewDidLoad. You can also use viewDidAppear: or viewWillAppear:.
You must override this methods in MyViewController class (which should inherit from the UIViewController class).
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.heigth);
imageView.image = [UIImage imageNamed#"stallion1.png"];
[self.view addSubview:imageView];
}

app works fine on iPhone; iPad simulator gives only black screen

Here's my applicationDidLaunch method from the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
//This hides the status bar throughout the app.
[UIApplication sharedApplication].statusBarHidden=YES;
NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:5];
GHHaikuViewController *hvc = [[GHHaikuViewController alloc] init];
hvc.tabBarItem.title = #"Home";
hvc.tabBarItem.image = [UIImage imageNamed:#"53-house.png"];
[tabItems addObject:hvc];
GHComposeViewController *cvc = [[GHComposeViewController alloc] init];
cvc.tabBarItem.title = #"Compose";
cvc.tabBarItem.image = [UIImage imageNamed:#"216-compose.png"];
[tabItems addObject:cvc];
GHWebViewController *wvc = [[GHWebViewController alloc] init];
wvc.tabBarItem.title = #"Buy";
wvc.tabBarItem.image = [UIImage imageNamed:#"80-shopping-cart.png"];
[tabItems addObject:wvc];
GHFeedback *fvc = [[GHFeedback alloc] init];
fvc.tabBarItem.title = #"Feedback";
fvc.tabBarItem.image = [UIImage imageNamed:#"18-envelope.png"];
[tabItems addObject:fvc];
GHSettingsViewController *svc = [[GHSettingsViewController alloc] init];
svc.tabBarItem.title = #"Settings";
svc.tabBarItem.image = [UIImage imageNamed:#"20-gear-2.png"];
[tabItems addObject:svc];
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = tabItems;
self.window.rootViewController = tbc;
return YES;
}
And here's viewDidLoad from the first view controller, simplified for clarity:
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
background.backgroundColor = [UIColor whiteColor];
[self.view addSubview:background];
NSLog(#"View loaded.");
}
Everything works fine on iPhone. On iPad (or at least simulator), "View loaded" logs, but instead of a white screen it's just black. Earlier I was playing around with a storyboard for the iPad, but I've deleted it and I THINK I removed all the files, and now everything is running from code. I've cleaned the project. What am I overlooking?
EDIT: Here's a shot of the target summary in Xcode.
If you don't use a main storyboard or xib you need to create the UIWindow in code:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
Make sure, that the storyboard for iPad is cleared in the project settings. IMO Xcode doesn't delete the entry when deleting a storyboard. You should only have storyboard for iPhone configured there.

Resources