Objc55
Objc55

Reputation: 156

Display Overlay on Image

Using this tutorial: http://www.techotopia.com/index.php/An_Example_iOS_4_iPhone_Camera_Application_%28Xcode_4%29 I have displayed on the UI an image from the photo library. I would like to simply overlay some basic text or a shape (like a square) over the UIImage. Is there an easy way to do this?

Thank you.

Upvotes: 1

Views: 4352

Answers (2)

hotpaw2
hotpaw2

Reputation: 70673

You can create a subclass of a UIView, make it transparent, and add an instance of this class, the same size as your image, as a overlay subview on top of the image. Then, when you want to display something on top of your image, call setNeedsDisplay on the transparent subview, and draw whatever you want (colored rectangles, text, etc.) in the drawRect callback of this view.

To make a UIView subclass transparent, add this to the view class initWithFrame method:

[ self setBackgroundColor:[ UIColor clearColor ] ];

Upvotes: 0

Hermann Klecker
Hermann Klecker

Reputation: 14068

Do you have an UIImage only or do you display it using an UIImageView?

If you have an UIImageView then you can just add a UILabel as sub view to your image view.

Assuming your UIImageView is named myImageView ...

// Create and init the label with any rect. 
UILabel *myLabel = [[UILabel alloc] initWithFrame:myImageView.frame]; // The lable has the same size as the Image has.
mylabel.text = @"the text";
[myImageView addSubview:myLabel];
//A subview's coordinates are within its superview's coordinates. 
mylabel.center = CGPointMake(myImageView.frame.size.width/2,myImageView.frame.size.height/2) // aligning the centers of the views
//An alternative could be transforming the image's center into the coordinate system of its superviews. That code would look smarter but his example is imho easier to understand.

//Setting front, size, alpha, background colour ... 
... 

Upvotes: 2

Related Questions