如何以编程方式添加图像以查看? [英] How to add image to view programmatically?
本文介绍了如何以编程方式添加图像以查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设你有一个 UIImage *image
和一个 UIView *v
Say you have a UIImage *image
and a UIView *v
您将如何以编程方式在视图顶部显示图像?
How would you display the image on top of the view programmatically?
推荐答案
如果你只是想把 UIImage 添加到 UIView 那么你需要在 UIView 和 UIImage 之间添加一个 UIImageView,例如:
If you just want to add the UIImage to the UIView then you need an UIImageView inbetween the UIView and UIImage, such as:
UIImage *image = [[UIImage alloc] init];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[iv setImage:image];
[v addSubview:iv];
请注意,以上只是虚拟代码,我已经使用零帧创建了所有 UI 元素.
Note that the above is just dummy code and I've created all UI elements with a zero frame.
这篇关于如何以编程方式添加图像以查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文