Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
Im trying to load an UIImageView into a view, but it wont appear. Any idea why?

Code:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
	// 1. Create View
	CGSize screenSize = [UIScreen mainScreen].bounds.size;
	CGRect screenBounds = CGRectMake(0, 0, screenSize.width, screenSize.height);	
	UIView *introView = [[UIView alloc] initWithFrame:screenBounds];			

	// 2. Show View
	[window addSubview:introView]; // window is a UIWindow-object in the applicationdelegate

	// 3. Create UIImage and add to View
	UIImageView *introAnimation = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 30.0f, 30.0f)];	
	NSArray *animationBeginning = [NSArray arrayWithObjects:
						  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon_no_sound_white_34.png" ofType:@"png"]],
						  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon_sound_white_34.png" ofType:@"png"]], nil];
	
	introAnimation.animationImages = animationBeginning;
	introAnimation.animationDuration = 2.0;
	introAnimation.animationRepeatCount = 0;
	[introView addSubview:introAnimation];
	[animationBeginning release];
	animationBeginning = nil;
	// start
	[introAnimation startAnimating];
}
 
You've made your UIImageView a subview of your intro UIView but you haven't attached the introView to any view on the screen, hence both are not being displayed.

Inserting
Code:
[self.view addSubview:introView];
after you initialise the introView should make it work.

Edit: oh I realised you're doing that in your app delegate. You should really have a view controller to control the view.
 
Sorry, sloppy writing on my behalf - added the second tag to the code, which adds the view to a UIWindow instead. Works fine for adding buttons, for instance..

You suggest that I do this in a UIViewController instead, then?
 
Sorry, sloppy writing on my behalf - added the second tag to the code, which adds the view to a UIWindow instead. Works fine for adding buttons, for instance..

You suggest that I do this in a UIViewController instead, then?

you should always use a UIViewController, but that doesn't cause your problem.

did you try to just create a UIImageView with "initWithImage:"? I know this isn't what you need, but just to see if that works. because if that imageview gets shown then the problem isn't your code logic or something but rather something with the UIImages or something.
 
I had included the .png extention to the pathforresource-string. After removing it it works great :)

Thanks for the help, though :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.