的iOS UIView的背景图像

问题描述:

我想有一个UIImage作为背景图像上UIView.的iOS UIView的背景图像

这里是我的代码:

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"login.png"]]; 
self.view.backgroundColor = background; 
[background release]; 

的问题是,该规模是不行的。它的缩放比例为640到480,所以我不能100%确定,但是看起来只有300到250个被显示或类似的东西。

是否适合缩放/适合UIView /适合大小的modus?

+0

原始PNG的大小是多少?你是否也在视网膜设备上观看此视频? – 2012-04-03 13:36:29

按照UIColor API:

可以使用图案的颜色设置填充或描边颜色,就像你纯色。在绘制过程中,图案颜色中的图像根据需要平铺以覆盖指定区域。

这意味着它会尝试从图像中创建一个模式来填充该区域。我认为最好的做法是重新调整图像以适应UIView的尺寸。

有一个很好的答案在这里如何在运行时调整图片大小:

The simplest way to resize an UIImage?

招呼试试这个,

 self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"login.png"]]; 
+0

不起作用... – Kovu 2012-04-03 13:47:54

+1

最佳答案,清除并立即运行。适合快速研究。 – Heckscheibe 2013-07-09 12:31:34

+0

无法正常工作。 – bapi 2013-11-29 05:25:10

指定的实际背景,并将其发送到您需要

//add background 
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]]; 
[myView addSubview:background]; 
[myView sendSubviewToBack:background]; 
myView.contentMode = UIViewContentModeScaleAspectFit; 

视图背面处理您的图像添加之前,试试这个:

UIGraphicsBeginImageContext(self.view.frame.size); 
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

self.view.backgroundColor = [UIColor colorWithPatternImage:image]; 
+0

谢谢,它对我很好 – swiftBoy 2013-08-29 04:37:17

+0

这是最正确的方法...为我工作..谢谢。 – bapi 2013-11-29 05:24:22

+2

在swift中:https://gist.github.com/asiviero/c9e63b3607bf58373cab – asiviero 2015-06-10 18:58:25

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image.png"]]; 
self.view.backgroundColor = background; 
[background release]; 

在Interface Builder中,将Image View添加到View,然后在Attributes inspector中选择要使用的图像。最后,通过拖动Document Outline中的元素使Image View成为View的第一个孩子。

如果视图都将有相同的大小,你可以把你的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]; 
} 

感谢@uneakharsh的帮助!

如果你想做到这一点在迅速:

self.view.backgroundColor = UIColor(patternImage: UIImage(named:"background.jpg")) 
+0

是的,我想在Swift中做:D – 2015-03-20 00:04:11

this.View.BackgroundColor = UIColor.FromPatternImage (UIImage.FromFile ("body_background.png")); 

您可以使用UIGraphicsBeginImageContext方法来设置图像的同时使得视大小。

语法: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")]; 

斯威夫特使用本...

UIGraphicsBeginImageContext(self.view.frame.size) 
    UIImage(named: "1.png")?.drawInRect(self.view.bounds) 

    var image: UIImage = UIGraphicsGetImageFromCurrentImageContext() 

    UIGraphicsEndImageContext() 

    self.view.backgroundColor = UIColor(patternImage: image) 

我使用以下扩展斯威夫特:

extension UIView{ 

    func setBackgroundImage(img: UIImage){ 

     UIGraphicsBeginImageContext(self.frame.size) 
     img.drawInRect(self.bounds) 
     let patternImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     self.backgroundColor = UIColor(patternImage: patternImage) 
    } 
} 

在代码中你可以使用这样的:

if let image = UIImage(named: "HomeBg"){ 
     self.view.setBackgroundImage(image) 
    } 

我也想做到这一点,我发现,由于自动版式,在UIImageView拉伸含UIView,当我想用​​另一种方式:我要UIImageView采取UIView的大小。

所以我创建了这个类。无论何时通过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:)") } 
}