假设您有UIImage *image
和UIView *v
如何以编程方式在视图顶部显示图像?
答案 0 :(得分:23)
如果你只想将UIImage添加到UIView,那么在UIView和UIImage之间需要一个UIImageView,例如:
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元素。
答案 1 :(得分:0)
如果要以编程方式将UIImage添加到UIView,可以直接添加图像进行查看,例如:
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
customView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"YourImage.png"]];
[self.view addSubview:customView];