2012-03-15 113 views
1

使用本教程:http://www.techotopia.com/index.php/An_Example_iOS_4_iPhone_Camera_Application_%28Xcode_4%29 我在界面上显示了图片库中的图像。我想简单地在UIImage上覆盖一些基本文字或形状(如方形)。是否有捷径可寻?在图像上显示覆盖图

谢谢。

+0

叠加在UIImage的一个视图。如果你想要文字,然后在图像后添加一个UILabel,然后设置框架,使其覆盖在正确的位置。对于形状只需使用适当的视图为你的形状,并做同样的事情。 – Joe 2012-03-15 18:06:08

+0

@Joe我怎么会在代码上做到这一点?谢谢。 – Objc55 2012-03-15 18:08:22

+0

对不起,我忘了有些人只能使用界面构建器才能得到。 [查看iOS编程指南](https://developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html#//apple_ref/doc/uid/TP40009503-CH5-SW6 ) – Joe 2012-03-15 20:11:46

回答

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 ... 
... 
+0

我有一个imageView中显示的图像,像这样:'UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage]; imageView.image = image;' – Objc55 2012-03-15 18:37:58

+0

好。在这种情况下,上面的代码是可行的。正如你所看到的,它将把Lable放在ImageView的中心。随意改变这一点。请记住,子视图坐标系的原点(0,0)位于其超视图的左上角。当然,您可能想要更改颜色,对齐等其他属性。 – 2012-03-15 18:42:44

+0

感谢您的回复。不幸的是,使用你的代码,文本确实会出现,但它应该出现在最后的图像消失,并被替换,所以我最终只在白色背景上显示黑色文本。 – Objc55 2012-03-15 18:47:45

0

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

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

[ self setBackgroundColor:[ UIColor clearColor ] ];