Question

upon a tap, I want to let a few UIImageView objects appear on an underlaying UIView. So I created the following action:

- (IBAction)startStars: (id) sender
{
UIView* that = (UIView*) sender;  // an UIButton, invisible, but reacting to Touch Down events
int tag = that.tag;

UIView* page = [self getViewByTag:mPages index:tag];  // getting a page currently being viewed
if ( page != nil )
{

    int i;
    UIImage* image = [UIImage imageNamed:@"some.png"];

    for ( i = 0 ; i < 10 ; ++i )
    {   
        // create a new UIImageView at the position of the tapped UIView
        UIImageView* zap = [[UIImageView alloc] initWithFrame:that.frame];
        // assigning the UIImage
        zap.image = image;

        // make a modification to the position so we can see all instances
        CGPoint start = that.layer.frame.origin;
        start.x += i*20;
        zap.layer.position = start;

        [page addSubview:zap];   // add to the UIView
        [zap setNeedsDisplay];   // please please redraw
        [zap release];           // release, since page will retain zap

    }
    [image release];
}
}

Unfortunately, nothing shows up. The code gets called, the objects created, the image is loaded, even the properties are as expected. Page itself is a real basic UIView, created with interface builder to contain other views and controls.

Still, nothing of this can be seen....

Has anyone an idea what I am doing wrong? Do I need to set the alpha property (or others)?

Thanks

Zuppa

Was it helpful?

Solution

A couple of things I would check:

  • Logging to make sure this section of code is actually being called.
  • Use the designated initializer for UIImageView:

    [[UIImageView alloc] initWithImage:image];
    

    This will ensure that your new view has the correct size for the image - what are the relative sizes of zap and your image? It could be out of the frame.

  • Ensure that the image actually exists and is created

  • Try not adjusting the layer properties, but setting the center of the image view instead. In fact, in the first instance don't adjust it at all and just see if you can see anything. I'm not sure but I think position might be moving the layer within the view so could be moving your image out of sight. Try:

    CGPoint newCenter = that.frame.origin;
    newCenter.y += zap.bounds.size.height/2;
    newCenter.x += i*20;
    zap.center = origin;
    
  • setNeedsDisplay is not required. Obviously from your comments it was an act of desparation!

OTHER TIPS

Is your UIImage nil? You don't need to have the .png extension for imageNamed: to work. It might not even work correctly if you put it in, I'm not sure. You're also overreleasing your image. imageNamed: returns an autoreleased image so there is no reason to call release on the image unless you're also calling retain on it somewhere.

I see a potential error: on second invocation you will re-add another copy, so take care about removing subviews before adding anew copy. Yu can alternatively move a previous view.

99% of my cases about non-visible views with images are wrong names, so as suggested, load using a temp var:

UIImage img = [UIImage imageNamed..];

and test img.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top