国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

博主教你制作類似9patch效果的iOS圖片拉伸

2019-11-14 19:52:46
字體:
供稿:網(wǎng)友

下面張圖片,本來是設(shè)計(jì)來做按鈕背景的:

  button.png,尺寸為:24x60

現(xiàn)在我們把它用作為按鈕背景,按鈕尺寸是150x50:

C代碼 復(fù)制代碼 收藏代碼
  1. // 得到view的尺寸  
  2. CGSize viewSize = self.view.bounds.size;  
  3.   
  4. // 初始化按鈕  
  5. UIButton *button = [[UIButton alloc] init];  
  6. // 設(shè)置尺寸  
  7. button.bounds = CGRectMake(0, 0, 150, 50);  
  8. // 設(shè)置位置  
  9. button.center = CGPointMake(viewSize.width * 0.5f, viewSize.height * 0.5f);  
  10.   
  11. // 加載圖片  
  12. UIImage *image = [UIImage imageNamed:@"button"];  
  13. // 設(shè)置背景圖片  
  14. [button setBackgroundImage:image forState:UIControlStateNormal];  
  15.   
  16. // 添加按鈕  
  17. [self.view addSubview:button];  
// 得到view的尺寸CGSize viewSize = self.view.bounds.size;// 初始化按鈕UIButton *button = [[UIButton alloc] init];// 設(shè)置尺寸button.bounds = CGRectMake(0, 0, 150, 50);// 設(shè)置位置button.center = CGPointMake(viewSize.width * 0.5f, viewSize.height * 0.5f);// 加載圖片UIImage *image = [UIImage imageNamed:@"button"];// 設(shè)置背景圖片[button setBackgroundImage:image forState:UIControlStateNormal];// 添加按鈕[self.view addSubview:button];

 

運(yùn)行效果圖:

 

可以看到,效果非常地差。原因很簡單,因?yàn)樵瓐D大小為24x60,現(xiàn)在整張圖片被全方位拉伸為150x50,比較嚴(yán)重的是圖片的4個(gè)角。

有些人可能馬上想到一個(gè)解決方案,你叫美工把圖片做大一點(diǎn)不就好了么,怎么拉伸都沒事。沒錯(cuò),這是一種解決方案,不過不建議采取。原因很簡單:1.圖片大,導(dǎo)致安裝包也大,加載到內(nèi)存中也大;2.有更好的解決方案。

細(xì)看一下圖片,其實(shí)圖片會(huì)變得難看,完全是因?yàn)?個(gè)角被拉伸了,中間的拉伸并沒有明顯地丑化外觀。因此要想小圖片被拉伸后不會(huì)變得難看,在圖片拉伸的時(shí)候,我們只需拉伸圖片的中間一塊矩形區(qū)域即可,不要拉伸邊緣部分。

比如只拉伸下圖的矩形區(qū)域,上下左右的邊緣都不拉伸:

 

iOS中提供很好用的API幫我們實(shí)現(xiàn)上述功能。到iOS 6.0為止,iOS提供了3種圖片拉伸的解決方案,接下來分別詳細(xì)介紹這些方案。

一、iOS 5.0之前

iOS中有個(gè)叫端蓋(end cap)的概念,用來指定圖片中的哪一部分不用拉伸。比如下圖中,黑色代表需要被拉伸的矩形區(qū)域,上下左右不需要被拉伸的邊緣就稱為端蓋。

 

使用UIImage的這個(gè)方法,可以通過設(shè)置端蓋寬度返回一個(gè)經(jīng)過拉伸處理的UIImage對象

java代碼 復(fù)制代碼 收藏代碼
  1. - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;  
 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;

 

這個(gè)方法只有2個(gè)參數(shù),leftCapWidth代表左端蓋寬度,topCapHeight代表頂端蓋高度。系統(tǒng)會(huì)自動(dòng)計(jì)算出右端蓋寬度(rightCapWidth)和底端蓋高度(bottomCapHeight),算法如下:

Java代碼 復(fù)制代碼 收藏代碼
  1. // width為圖片寬度  
  2. rightCapWidth = width - leftCapWidth - 1;  
  3.   
  4. // height為圖片高度  
  5. bottomCapHeight = height - topCapHeight - 1  
// width為圖片寬度rightCapWidth = width - leftCapWidth - 1;// height為圖片高度bottomCapHeight = height - topCapHeight - 1

 

經(jīng)過計(jì)算,你會(huì)發(fā)現(xiàn)中間的可拉伸區(qū)域只有1x1

Java代碼 復(fù)制代碼 收藏代碼
  1. // stretchWidth為中間可拉伸區(qū)域的寬度  
  2. stretchWidth = width - leftCapWidth - rightCapWidth = 1;  
  3.       
  4. // stretchHeight為中間可拉伸區(qū)域的高度  
  5. stretchHeight = height - topCapHeight - bottomCapHeight = 1;  
// stretchWidth為中間可拉伸區(qū)域的寬度stretchWidth = width - leftCapWidth - rightCapWidth = 1;    // stretchHeight為中間可拉伸區(qū)域的高度stretchHeight = height - topCapHeight - bottomCapHeight = 1;

 

因此,使用這個(gè)方法只會(huì)拉伸圖片中間1x1的區(qū)域,并不會(huì)影響到邊緣和角落。

下面演示下方法的使用:

Java代碼 復(fù)制代碼 收藏代碼
  1. // 左端蓋寬度  
  2. NSInteger leftCapWidth = image.size.width * 0.5f;  
  3. // 頂端蓋高度  
  4. NSInteger topCapHeight = image.size.height * 0.5f;  
  5. // 重新賦值  
  6. image = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];  
// 左端蓋寬度NSInteger leftCapWidth = image.size.width * 0.5f;// 頂端蓋高度NSInteger topCapHeight = image.size.height * 0.5f;// 重新賦值image = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];

 

調(diào)用這個(gè)方法后,原來的image并不會(huì)發(fā)生改變,會(huì)產(chǎn)生一個(gè)新的經(jīng)過拉伸的UIImage,所以第6行中需要將返回值賦值回給image變量

運(yùn)行效果:

 可以發(fā)現(xiàn),圖片非常美觀地顯示出來了

注意:

1.這個(gè)方法在iOS 5.0出來后就過期了

2.這個(gè)方法只能拉伸1x1的區(qū)域

 

二、iOS 5.0

在iOS 5.0中,UIImage又有一個(gè)新方法可以處理圖片的拉伸問題

Java代碼 復(fù)制代碼 收藏代碼
  1. - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets  
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

 

這個(gè)方法只接收一個(gè)UIEdgeInsets類型的參數(shù),可以通過設(shè)置UIEdgeInsets的left、right、top、bottom來分別指定左端蓋寬度、右端蓋寬度、頂端蓋高度、底端蓋高度

Java代碼 復(fù)制代碼 收藏代碼
  1. CGFloat top = 25; // 頂端蓋高度  
  2. CGFloat bottom = 25 ; // 底端蓋高度  
  3. CGFloat left = 10; // 左端蓋寬度  
  4. CGFloat right = 10; // 右端蓋寬度  
  5. UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);  
  6. // 伸縮后重新賦值  
  7. image = [image resizableImageWithCapInsets:insets];  
CGFloat top = 25; // 頂端蓋高度CGFloat bottom = 25 ; // 底端蓋高度CGFloat left = 10; // 左端蓋寬度CGFloat right = 10; // 右端蓋寬度UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);// 伸縮后重新賦值image = [image resizableImageWithCapInsets:insets];

 

運(yùn)行效果:

   

 

三、iOS 6.0

在iOS6.0中,UIImage又提供了一個(gè)方法處理圖片拉伸

 

Java代碼 復(fù)制代碼 收藏代碼
  1. - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode  
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode

 

對比iOS5.0中的方法,只多了一個(gè)UIImageResizingMode參數(shù),用來指定拉伸的模式:

  • UIImageResizingModeStretch:拉伸模式,通過拉伸UIEdgeInsets指定的矩形區(qū)域來填充圖片
  • UIImageResizingModeTile:平鋪模式,通過重復(fù)顯示UIEdgeInsets指定的矩形區(qū)域來填充圖片
Java代碼 復(fù)制代碼 收藏代碼
  1. CGFloat top = 25; // 頂端蓋高度  
  2. CGFloat bottom = 25 ; // 底端蓋高度  
  3. CGFloat left = 10; // 左端蓋寬度  
  4. CGFloat right = 10; // 右端蓋寬度  
  5. UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);  
  6. // 指定為拉伸模式,伸縮后重新賦值  
  7. image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];  
CGFloat top = 25; // 頂端蓋高度CGFloat bottom = 25 ; // 底端蓋高度CGFloat left = 10; // 左端蓋寬度CGFloat right = 10; // 右端蓋寬度UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);// 指定為拉伸模式,伸縮后重新賦值image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];

運(yùn)行效果:


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 文昌市| 淳安县| 谷城县| 肇源县| 印江| 肃宁县| 大同市| 宜黄县| 增城市| 乌拉特前旗| 如皋市| 竹山县| 林甸县| 都安| 弋阳县| 宝丰县| 安塞县| 伊金霍洛旗| 柳河县| 伊吾县| 定襄县| 英德市| 汉阴县| 长顺县| 普宁市| 山阳县| 新余市| 长丰县| 鱼台县| 广水市| 玉环县| 高雄县| 监利县| 大英县| 蓝山县| 辛集市| 乐安县| 香格里拉县| 宁蒗| 建湖县| 大丰市|