UIView 背景图片调整大小 [英] UIView background image resize
本文介绍了UIView 背景图片调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 UIView(这是我的导航抽屉),我想设置一个背景图像.首先我尝试过这种方法:
I have a UIView (that is my navigation drawer) and I want to set a background image. First I have tried this approach:
menuView.backgroundColor = UIColor(patternImage: UIImage(named: "menu_image.png")!)
但是我只得到了填充 UIView 的图像的一部分.所以接下来我尝试像这样调整图像大小:
But I get only a part of my image that fills my UIView. So next I have tried to resize image like this:
UIGraphicsBeginImageContext(menuView.frame.size);
var image = UIImage(named: "menu_image.png")
image?.drawInRect(menuView.bounds)
UIGraphicsEndImageContext()
menuView.backgroundColor = UIColor(patternImage: image!)
但这并没有帮助,而且奇怪的是,bcs 的人说,它帮助了他们.我不明白我做错了什么.
But it doesn't help and it is strange, bcs people said, that it helped them. I don't understand what I am doing wrong.
更新
我忘记了 1 行:
UIGraphicsBeginImageContext(menuView.frame.size);
var image = UIImage(named: "menu_image.png")
image?.drawInRect(menuView.bounds)
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
menuView.backgroundColor = UIColor(patternImage: image!)
问题已结束,谢谢.
推荐答案
Obj-C ... 将其转换为 Swift 就可以了(你少了一行)
Obj-C ... convert it to Swift and it will work (u're missing one line)
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
这篇关于UIView 背景图片调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文