在图像上显示叠加

时间:2012-03-15 17:57:34

标签: iphone objective-c ios xcode camera

使用本教程:http://www.techotopia.com/index.php/An_Example_iOS_4_iPhone_Camera_Application_%28Xcode_4%29 我在UI上显示了照片库中的图像。我想简单地在UIImage上覆盖一些基本文本或形状(如正方形)。有一个简单的方法吗?

谢谢。

2 个答案:

答案 0 :(得分:2)

您是仅使用UIImage还是使用UIImageView显示它?

如果您有UIImageView,那么您只需将UILabel作为子视图添加到图像视图中。

假设您的UIImageView名为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 ... 
... 

答案 1 :(得分:0)

您可以创建UIView的子类,使其透明,并添加此类的实例(与图像大小相同)作为图像顶部的叠加子视图。然后,当您想在图像上显示某些内容时,在透明子视图上调用setNeedsDisplay,并在此视图的drawRect回调中绘制您想要的任何内容(彩色矩形,文本等)。

要使UIView子类透明,请将其添加到视图类initWithFrame方法中:

[ self setBackgroundColor:[ UIColor clearColor ] ];