UIImage圖片拉伸問題

- (UIImage *)stretchableImageWithLeft
CapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

這個函數是UIImage的一個實例函數,它的功能是建立一個內容可拉伸,而邊角不拉伸的圖片,須要兩個參數,第一個是左邊不拉伸區域的寬度,第二個參數是上面不拉伸的高度。ide

根據設置的寬度和高度,將接下來的一個像素進行左右擴展和上下拉伸。函數

注意:可拉伸的範圍都是距離leftCapWidth後的1豎排像素,和距離topCapHeight後的1橫排像素。code

參數的意義是,若是參數指定10,5。那麼,圖片左邊10個像素,上邊5個像素。不會被拉伸,x座標爲11和一個像素會被橫向複製,y座標爲6的一個像素會被縱向複製。注意:只是對一個像素進行復制到必定寬度。而圖像後面的剩餘像素也不會被拉伸。orm


UIButton *btnLogin = [UIButton buttonWithType:UIButtonTypeSystem];
btnLogin.frame =  CGRectMake(60, [[UIScreen mainScreen] bounds].size.height -100, 100, 50);
[btnLogin.layer setCornerRadius:20];
UIImage *imgLogin = [UIImage imageNamed:@"LoginGreenBigBtn"];
imgLogin = [imgLogin stretchableImageWithLeftCapWidth:floorf(imgLogin.size.width/2) topCapHeight:floorf(imgLogin.size.height/2)];
[btnLogin setBackgroundImage:imgLogin forState:UIControlStateNormal];
[btnLogin setTitle:@"登陸" forState:UIControlStateNormal];
[btnLogin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLogin addTarget:self action:@selector(Login) forControlEvents:UIControlEventTouchUpInside];
[ImageView addSubview:btnLogin];

原來是這樣的:    圖片

圖片是這樣的:get

設置後,圖片顯示是這樣的:it

相關文章
相關標籤/搜索