[英]iOS UIView Background Image
I want to have a UIImage
as a background image on a UIView.
我希望在
UIView.
上有一个UIImage
作为背景图像UIView.
Here is my code: 这是我的代码:
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"login.png"]];
self.view.backgroundColor = background;
[background release];
The Problem is, that the scale is not okay. 问题是,规模不合适。 It scaled 640 to 480, so I can't tell it 100% sure but it looks like only 300 to 250 is displayed or something like that.
它缩放到640到480,所以我不能100%确定它,但它看起来只显示300到250或类似的东西。
Is there a fit to scale / fit to UIView / fit to size modus? 是否适合缩放/适合UIView /适合大小模式?
you are required to process your image before you add it, try this : 您需要在添加之前处理图像,请尝试以下操作:
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
你好试试这个,
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"login.png"]];
Specify an actual background and send it to the back of your view 指定实际背景并将其发送到视图的背面
//add background
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
[myView addSubview:background];
[myView sendSubviewToBack:background];
myView.contentMode = UIViewContentModeScaleAspectFit;
In Swift use this... 在Swift中使用这个......
UIGraphicsBeginImageContext(self.view.frame.size)
UIImage(named: "1.png")?.drawInRect(self.view.bounds)
var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.view.backgroundColor = UIColor(patternImage: image)
According to the UIColor
API: 根据
UIColor
API:
You can use pattern colors to set the fill or stroke color just as you would a solid color.
您可以使用图案颜色来设置填充或描边颜色,就像使用纯色一样。 During drawing, the image in the pattern color is tiled as necessary to cover the given area.
在绘制期间,图案颜色中的图像根据需要平铺以覆盖给定区域。
That means it tries to create a pattern out of your image to fill the area. 这意味着它会尝试从图像中创建一个图案来填充该区域。 I think the optimal way to do it is so rescale your image to fit exactly the
UIView
size. 我认为最好的方法是重新调整图像尺寸以适应
UIView
大小。
There is a nice answer here how to resize your image during runtime: 这里有一个很好的答案如何在运行时调整图像大小:
The simplest way to resize an UIImage? 调整UIImage大小的最简单方法是什么?
如果你想在swift中做到这一点:
self.view.backgroundColor = UIColor(patternImage: UIImage(named:"background.jpg"))
I'm using the following extension in Swift: 我在Swift中使用以下扩展:
extension UIView{
func setBackgroundImage(img: UIImage){
UIGraphicsBeginImageContext(self.frame.size)
img.drawInRect(self.bounds)
let patternImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.backgroundColor = UIColor(patternImage: patternImage)
}
}
In code you can use like this: 在代码中你可以像这样使用:
if let image = UIImage(named: "HomeBg"){
self.view.setBackgroundImage(image)
}
In the Interface Builder, add an Image View
to View
and select the image you want to use in Attributes inspector
. 在Interface Builder中,将“
Image View
添加到“ View
然后在“ Attributes inspector
选择要使用的图像。 Finally, make the Image View
the very first child of the View
by dragging the element in Document Outline
. 最后,通过拖动“
Document Outline
的元素,使“ Image View
”成为Image View
的第一个子View
。
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image.png"]];
self.view.backgroundColor = background;
[background release];
If the view is going to have the same size, you can place a method in your AppDelegate.m
that scales the background image on first run, and then keeps the scaled image in memory for future use: 如果视图具有相同的大小,您可以在
AppDelegate.m
中放置一个方法,在第一次运行时缩放背景图像,然后将缩放的图像保留在内存中以备将来使用:
UIImage *backgroundImage = nil;
- (void)setBackground:(UIView *)view
{
if (backgroundImage == nil)
{
UIGraphicsBeginImageContext(view.frame.size);
[[UIImage imageNamed:@"Background"] drawInRect:view.bounds];
backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
}
Thanks to @uneakharsh for his help! 感谢@uneakharsh的帮助!
You can use UIGraphicsBeginImageContext method to set the size of image same that of view. 您可以使用UIGraphicsBeginImageContext方法设置与视图相同的图像大小。
Syntax : void UIGraphicsBeginImageContext(CGSize size); 语法: void UIGraphicsBeginImageContext(CGSize size);
#define IMAGE(imageName) (UIImage *)[UIImage imageWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:imageName ofType:IMAGE_TYPE_PNG]]
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@“MyImage.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:IMAGE(@"mainBg")];
I also wanted to do this and I found that, due to autolayout , the UIImageView
stretches the containing UIView
, when I want it the other way: I want the UIImageView
to take the size of the UIView
. 我也想这样做,我发现,由于自动布局 ,
UIImageView
拉伸包含UIView
,当我想要它的另一种方式:我希望UIImageView
采用UIView
的大小。
So I created this class. 所以我创建了这个类。 It updates the
UIImageView
frame according to the UIView
container whenever it is laid out via layoutSubviews
. 每当通过
layoutSubviews
布局时,它都会根据UIView
容器更新UIImageView
框架。
/**
* View with an image whose frame is adjusted to the frame of the view.
*/
class ViewWithImage: UIView {
let image: UIImageView
init(image: UIImageView) {
self.image = image
super.init(frame: .zero)
self.addSubview(image)
}
override func layoutSubviews() {
super.layoutSubviews()
updateImageFrame()
}
private func updateImageFrame() {
image.frame = CGRect(origin: .zero, size: self.frame.size)
}
required init?(coder: NSCoder) { fatalError("no init(coder:)") }
}
this.View.BackgroundColor = UIColor.FromPatternImage (UIImage.FromFile ("body_background.png"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.