如何直接在 UIView 上设置 Image?

发布于 2024-12-10 19:51:32 字数 171 浏览 0 评论 0原文

谁能告诉我如何直接在 UIView 上设置 Image ? 这是我的代码:

 UIView *view=[[UIView alloc]init];
 [view setImage:[UIImage imageNamed:@"image.png"]];

Can any body tell me how can I set Image directly on UIView ?
Here is my code:

 UIView *view=[[UIView alloc]init];
 [view setImage:[UIImage imageNamed:@"image.png"]];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

嗫嚅 2024-12-17 19:51:32
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"a.png"]]];

希望这对您有帮助。

UIView *view=[[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"a.png"]]];

Hope this is help you.

寄居者 2024-12-17 19:51:32

这非常简单。尝试:

UIView * view = [[UIView alloc] initWithFrame:CGRectMake(origin_x, origin_y, width, height)]; 
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a.png"]];
[view addSubview:imageView];
[imageView release];

您还可以使用 UIImageViewframe 属性来在 UIView 中移动它。希望有帮助!

This is very simple to do. Try:

UIView * view = [[UIView alloc] initWithFrame:CGRectMake(origin_x, origin_y, width, height)]; 
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a.png"]];
[view addSubview:imageView];
[imageView release];

You can also play with the UIImageView's frame property to move it around in your UIView. Hope that Helps!

揪着可爱 2024-12-17 19:51:32
   UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        [imageView setImage:[UIImage imageNamed:@"320_480clouds_background.png"]];
        self.tableView.backgroundView = imageView;
        [imageView release];

希望这对您有帮助

   UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        [imageView setImage:[UIImage imageNamed:@"320_480clouds_background.png"]];
        self.tableView.backgroundView = imageView;
        [imageView release];

Hope this help you

没企图 2024-12-17 19:51:32

斯威夫特版本:

let view = UIView(frame: CGRectMake(0, 0, 640, 200))
view.backgroundColor = UIColor(patternImage: UIImage(named: "myImage")!)

斯威夫特 3:

let view = UIView(frame: CGRect(x: 0, y: 0, width: 640, height: 200))
view.backgroundColor = UIColor(patternImage: UIImage(named: "myImage")!)

Swift version :

let view = UIView(frame: CGRectMake(0, 0, 640, 200))
view.backgroundColor = UIColor(patternImage: UIImage(named: "myImage")!)

Swift 3:

let view = UIView(frame: CGRect(x: 0, y: 0, width: 640, height: 200))
view.backgroundColor = UIColor(patternImage: UIImage(named: "myImage")!)
西瑶 2024-12-17 19:51:32

你可以试试这个:

UIView *aView=[[UIView alloc]init];

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"anImage.png"]];
aView.backgroundColor = background;
[background release];

you can try this :

UIView *aView=[[UIView alloc]init];

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"anImage.png"]];
aView.backgroundColor = background;
[background release];
魔法唧唧 2024-12-17 19:51:32

在这里,我将 CGImage 分配给接受 CGImage 或 NSImage 的图层的内容属性。您只能将 CGImage 或 NSImage 分配给内容属性。

layerView.layer.contents = UIImage(named: "Mario.png")?.cgImage

Here I am assigning the CGImage to the contents property of layer which accepts CGImage or NSImage. You can only assign CGImage or NSImage to contents property.

layerView.layer.contents = UIImage(named: "Mario.png")?.cgImage
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文