iOS UIView 背景图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9994413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS UIView Background Image
提问by Kovu
I want to have a UIImage
as a background image on a 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/适合尺寸模式?
采纳答案by GooKSL
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:
如何在运行时调整图像大小这里有一个很好的答案:
回答by uneakharsh
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];
回答by Dipak Chaudhari
hello try this,
你好试试这个,
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"login.png"]];
回答by girl_meets_droid
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;
回答by Zaid Pathan
In Swiftuse 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)
回答by Kevin Delord
And if you want to do it in swift:
如果你想快速完成:
self.view.backgroundColor = UIColor(patternImage: UIImage(named:"background.jpg"))
回答by Hossam Ghareeb
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)
}
回答by Dino Tw
In the Interface Builder, add an Image View
to View
and select the image you want to use in Attributes inspector
. Finally, make the Image View
the very first child of the View
by dragging the element in Document Outline
.
在 Interface Builder 中,添加Image View
到View
并选择要在 中使用的图像Attributes inspector
。最后,通过拖动 中的元素来制作 的Image View
第一个子View
元素Document Outline
。
回答by Zorayr
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 的帮助!
回答by Manu P
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image.png"]];
self.view.backgroundColor = background;
[background release];