简体   繁体   English

我怎么能直接在UIView上设置Image?

[英]how can i set Image directly on UIView?

Can any body tell me how can I set Image directly on UIView ? 任何人都可以告诉我如何在UIView直接设置Image? Here is my code: 这是我的代码:

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

Hope this is help you. 希望这对你有所帮助。

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 . 您还可以使用UIImageViewframe属性在UIView移动它。 Hope that Helps! 希望有帮助!

   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 希望这对你有所帮助

Swift version : Swift版本:

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

Swift 3: 斯威夫特3:

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

you can try this : 你可以试试这个:

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

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

Here I am assigning the CGImage to the contents property of layer which accepts CGImage or NSImage. 这里我将CGImage分配给接受CGImage或NSImage的图层的contents属性。 You can only assign CGImage or NSImage to contents property. 您只能将CGImage或NSImage分配给contents属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM