我有一个UIView
,我以这种方式设置了背景图片:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sfond-appz.png"]];
我的问题是,背面图像不在视图内部居中,但已重播了几次以填充所有视图。有没有一种方法可以将图像集中在uiview和scretch中以具有屏幕大小?
注意:我不能UIImageView
用于背景,因为我有个scrollview
。
Answers:
您需要事先处理图像,以使图像居中并拉伸。尝试这个:
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
对于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")
}
该colorWithPattern:
方法实际上是根据图像生成图案。因此,您所需的自定义很可能是不可能的,也不是不可能的。
实际上,您需要使用UIImageView
来居中和缩放图像。您拥有的事实UIScrollView
并不能阻止这种情况:
创建self.view
一个通用视图,然后添加UIImageView
和UIScrollView
子视图。确保所有接口在Interface Builder中正确连接,并使滚动视图的背景颜色透明。
这是恕我直言,最简单,最灵活的设计,可以应对未来的变化。
重复:
UIImage *img = [UIImage imageNamed:@"bg.png"];
view.backgroundColor = [UIColor colorWithPatternImage:img];
拉长的
UIImage *img = [UIImage imageNamed:@"bg.png"];
view.layer.contents = img.CGImage;
在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!)
}
您可以使用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")];
对于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)
标记的答案很好,但是可以拉伸图像。在我的情况下,我有一个小的图块图像,我想重复进行而不拉伸。以下代码是我解决黑色背景问题的最佳方法:
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];
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)
}
}
}