【发布时间】:2015-01-24 14:25:48
【问题描述】:
我将视图的背景设置为图像。为此使用以下代码。但是图像似乎被拉长了。我怎样才能得到原来的图像?
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];
【问题讨论】:
-
图片的分辨率是多少? .3.5&4寸屏手机应该是320*480或者320*568
我将视图的背景设置为图像。为此使用以下代码。但是图像似乎被拉长了。我怎样才能得到原来的图像?
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];
【问题讨论】:
不建议像这样设置UIView 背景颜色(设置图像),而是添加UIImageView 作为UIView 的子视图,然后将图像分配给它。这看起来就像您设置了背景一样。
但是,这是解决方案,
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
直接来自this answer
【讨论】:
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"myImage.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
试试这个代码。它对我有用。
【讨论】: