如何填写UIView的背景图片

时间:2011-11-10 10:00:42

标签: iphone objective-c uiview background

我有一个UIView,我以这种方式设置背景图片:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sfond-appz.png"]];

我的问题是背景图像不在视图中居中,但它会重放几次以填充所有视图。有没有办法将图像置于uiview内部并将scretch设置为具有屏幕尺寸?

注意:我无法使用UIImageView作为背景原因我有scrollview

10 个答案:

答案 0 :(得分:278)

您需要事先处理图像,以制作居中且拉伸的图像。 试试这个:

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

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

答案 1 :(得分:54)

对于 Swift ,请使用此...

UIGraphicsBeginImageContext(self.view.frame.size)
UIImage(named: "ImageName.png")?.draw(in: self.view.bounds)

if let image = UIGraphicsGetImageFromCurrentImageContext(){
    UIGraphicsEndImageContext()
    self.view.backgroundColor = UIColor(patternImage: image)
}else{
    UIGraphicsEndImageContext()
    debugPrint("Image not available")
 }

答案 2 :(得分:17)

colorWithPattern:方法实际上是用于从图像生成模式。因此,您需要的定制很可能是不可能的,也不是可能的。

确实,您需要使用UIImageView来居中和缩放图像。您拥有UIScrollView的事实并不能阻止这种情况:

self.view设为通用视图,然后将UIImageViewUIScrollView添加为子视图。确保在Interface Builder中正确连接所有内容,并使滚动视图的背景颜色透明。

这是恕我直言,是未来变化的最简单,最灵活的设计。

答案 3 :(得分:15)

重复:

UIImage *img = [UIImage imageNamed:@"bg.png"];
view.backgroundColor = [UIColor colorWithPatternImage:img];

<强>拉伸

UIImage *img = [UIImage imageNamed:@"bg.png"];
view.layer.contents = img.CGImage;

答案 4 :(得分:3)

对于Swift 2.1使用此...

    UIGraphicsBeginImageContext(self.view.frame.size)
    UIImage(named: "Cyan.jpg")?.drawInRect(self.view.bounds)

    let image: UIImage! = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

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

答案 5 :(得分:2)

Swift 4 中做正确的方法, 如果您的框架作为屏幕尺寸正确,那么放在任何地方,  在viewDidLayoutSubviews中写这个很重要,因为我们可以在viewDidLayoutSubviews

中获得实际框架
   override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        UIGraphicsBeginImageContext(view.frame.size)
        UIImage(named: "myImage")?.draw(in: self.view.bounds)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        view.backgroundColor = UIColor.init(patternImage: image!)
    }

答案 6 :(得分:1)

您可以使用 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")];

答案 7 :(得分:1)

对于Swift 3.0,请使用以下代码:

    UIGraphicsBeginImageContext(self.view.frame.size)
    UIImage(named: "bg.png")?.drawAsPattern(in: self.view.bounds)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    self.view.backgroundColor = UIColor(patternImage: image)

答案 8 :(得分:1)

标记的答案很好,但它会使图像拉长。 在我的情况下,我有一个小瓷砖图像,我想重复不拉伸。 以下代码是解决黑色背景问题的最佳方式:

UIImage *tileImage = [UIImage imageNamed:@"myTileImage"];
UIColor *color = [UIColor colorWithPatternImage:tileImage];
UIView  *backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
[backgroundView setBackgroundColor:color];
//backgroundView.alpha = 0.1; //use this if you want to fade it away.
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];

答案 9 :(得分:0)

Swift 4解决方案:

@IBInspectable var backgroundImage: UIImage? {
    didSet {
        UIGraphicsBeginImageContext(self.frame.size)
        backgroundImage?.draw(in: self.bounds)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        if let image = image{
            self.backgroundColor = UIColor(patternImage: image)
        }
    }
}