2017-06-06 53 views
1

我想設置圖像沒有任何uiimageview,現在我有圖像,但它是水平重複兩次我想縮放和修復它到屏幕大小你如何做到這一點??如何縮放以適合背景圖像的目標c

我已經嘗試了一些方法,但沒有制定出

下面

是我的代碼:

- (void)viewDidLoad { 
[super viewDidLoad]; 
UIGraphicsBeginImageContext(self.view.frame.size); 
[[UIImage imageNamed:@"background_themee.png"] drawInRect:self.view.bounds]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
// self.view.backgroundColor = [UIColor colorWithPatternImage:image]; 
// UIGraphicsBeginImageContext(self.view.frame.size); 
// [[UIImage imageNamed:@"background_themee.png"] drawInRect:self.view.bounds]; 
// UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
// UIGraphicsEndImageContext(); 
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_themee"]]; 
// Do any additional setup after loading the view. 
} 

在此先感謝您的建議!

+0

簡單的這條線就足夠了'self.view.backgroundColor =的UIColor colorWithPatternImage:[UIImage的imageNamed:@「background_themee 「]];' –

+0

@GeneCode ok讓我試試吧! – Akshay

回答

1

colorWithPattern將始終在視圖上重複圖像。如果您在iPhone中打開,它可能會出現2倍的圖像。如果你在iPad上運行,可能會顯示4張圖片。所以它對背景圖像不好。

你需要的是一個UIImageView,其中包含正確設置的約束和contentMode設置爲AspectFill。

+1

是的,它工作謝謝你!你可以投票,如果你認爲這是一個有效的問題:) – Akshay

1

enter image description here

可以選擇規模填寫或填寫方面/ FIT根據您的需要