UIImageView not appearing

Discussion in 'iOS Programming' started by Danneman101, May 28, 2009.

  1. Danneman101 macrumors 6502

    Aug 14, 2008
    Im trying to load an UIImageView into a view, but it wont appear. Any idea why?

    - (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];
  2. johnnyjibbs macrumors 68030


    Sep 18, 2003
    London, UK
    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.

    [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.
  3. Danneman101 thread starter macrumors 6502

    Aug 14, 2008
    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?
  4. BlackWolf macrumors regular

    Apr 9, 2009
    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.
  5. Danneman101 thread starter macrumors 6502

    Aug 14, 2008
    I had included the .png extention to the pathforresource-string. After removing it it works great :)

    Thanks for the help, though :)

Share This Page