Wilson Tang’s Blog

I am a slow walker, but I never walk backwards.

如何填补UIView的背景图片

| Comments

想设置如下一张图片填充为self.view的背景

开始使用的方法是:

self.contentView.backgroundColor = UIColor(patternImage: UIImage(named: "selfCheck_bkg")!)

效果变成了: 明显不是我想要得到的填充效果。

解决办法,使用如下代码:

1
2
3
4
5
UIGraphicsBeginImageContext (self.view.frame.size);
UIImage(named: "selfCheck_bkg")?.drawInRect(self.view.bounds)
var bgImage:UIImage! = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.view.backgroundColor = UIColor(patternImage: bgImage)

得到的效果变成了:

Comments