Luke Irvin
Luke Irvin

Reputation: 1249

Draw Over Image

I'm working on some drawing code. I have that portion working great.

I want to draw over an image, but I want to still be able to see the detail of the image, the black lines, etc.

What I am working on is making a transparent UIImageView that holds the image.

I'm not sure how to get this set up properly though.

Should this be added above the other UIImageView that I color on or below it?

Here's what I have so far:

- (void)viewDidLoad
{
    [super viewDidLoad];

    topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 46, 320, 370)];
    [topImageView setImage:[UIImage imageNamed:@"imagesmall.png"]];
    topImageView.alpha = 1.0;
    topImageView.layer.opacity = 1.0;
    topImageView.layer.opaque = NO;
    [self.view addSubview:topImageView];
    [topImageView release];
}

Thoughts anyone?

Upvotes: 2

Views: 819

Answers (1)

Cowirrie
Cowirrie

Reputation: 7226

Yes, you can draw views over other views. They are drawn in the order that they're added as subviews, unless you reorder them after that.

You may need to set the opaque property for some views (this is distinct from and overrides their layer opacity), and set their backgroundColor to nil. UIImageView seems to be transparent by default, as long as its image is; some other UIView subclasses are not.

So, just what is your overlay going to be? If you just need to display one image over another, what you have here seems to work already. If you need to draw some lines programmatically, you'll need to do this:

  1. Create a subclass of UIView.
  2. Implement its drawRect method to display the content you need.
  3. When you add your custom view on top of the background image, make sure it is not opaque and has no backgroundColor.

A common problem here is to find that your foreground is working, but the background isn't being loaded properly. To make sure the background is there, set the alpha of the foreground view to 0.5. You won't want to do that in production, but it will allow you to verify that both views exist.

Upvotes: 1

Related Questions