Jak wypełnić obraz tła UIView
Mam UIView
i ustawiam obrazek tła w ten sposób:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sfond-appz.png"]];
Mój problem polega na tym, że back-image nie jest wyśrodkowany wewnątrz widoku, ale jest odtwarzany kilka razy, aby wypełnić cały widok. Czy istnieje sposób na wyśrodkowanie obrazu wewnątrz uiview i scartch, aby uzyskać rozmiar ekranu?
Uwaga: nie mogę użyć UIImageView
dla tła, ponieważ mam scrollview
.
10 answers
Musisz wcześniej przetworzyć obraz, Aby uzyskać wyśrodkowany i rozciągnięty obraz. Spróbuj tego:
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-10 10:10:20
Dla Swift Użyj tego...
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")
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-24 13:59:52
Metoda colorWithPattern:
służy do generowania wzorców z obrazów. Tak więc dostosowanie, którego potrzebujesz, najprawdopodobniej nie jest możliwe, ani nie ma być.
UIImageView
. To, że masz UIScrollView
nie przeszkadza:
Stwórz self.view
ogólny widok, a następnie dodaj zarówno UIImageView
, jak i UIScrollView
jako podview. Upewnij się, że wszystko jest poprawnie podłączone w interfejsie Builder i upewnij się, że kolor tła widoku przewijania jest przezroczysty.
To jest IMHO najprostszy i najbardziej elastyczny projekt dla przyszłych zmian.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-16 15:28:21
Repeat:
UIImage *img = [UIImage imageNamed:@"bg.png"];
view.backgroundColor = [UIColor colorWithPatternImage:img];
Rozciągnięty
UIImage *img = [UIImage imageNamed:@"bg.png"];
view.layer.contents = img.CGImage;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-31 07:55:34
Dla Swift 2.1 użyj tego...
UIGraphicsBeginImageContext(self.view.frame.size)
UIImage(named: "Cyan.jpg")?.drawInRect(self.view.bounds)
let image: UIImage! = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.view.backgroundColor = UIColor(patternImage: image)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-09 06:44:58
Poprawny sposób wykonania w Swift 4 ,
Jeśli rozmiar ramki jako ekranu jest prawidłowy, umieść w innym miejscu,
ważne aby to napisać w viewDidLayoutSubviews
ponieważ możemy uzyskać rzeczywistą ramkę w 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!)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-09 04:11:45
Możesz użyć metody UIGraphicsBeginImageContext aby ustawić rozmiar obrazu taki sam jak w widoku.
Składnia: 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")];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-06 07:13:54
Dla Swift 3.0 użyj następującego kodu:
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)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-19 04:36:39
Zaznaczona odpowiedź jest w porządku, ale sprawia, że obraz jest rozciągnięty. W moim przypadku miałem mały obraz płytki, który chciałem powtórzyć, a nie rozciągnąć. A poniższy kod był dla mnie najlepszym sposobem na rozwiązanie problemu z czarnym tłem:
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];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-22 20:46:00
Swift 4 Rozwiązanie:
@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)
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-18 08:19:40