自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS圖片拉伸:resizableImageWithCapInsets

移動(dòng)開發(fā) iOS
今天做了一個(gè)溫度計(jì)的應(yīng)用,需要一個(gè)圖,能夠根據(jù)輸入的數(shù)據(jù)將溫度計(jì)里面的紅色圖片拉伸。為了達(dá)到這個(gè)效果,使用了iOS5的函數(shù):resizableImageCapInsets:(UIEdgeInsets)Insets。最近終于申請(qǐng)到蘋果開發(fā)者賬號(hào)!搞的好煩??!給大家?guī)Ц@耍?/div>

今天做了一個(gè)溫度計(jì)的應(yīng)用,需要一個(gè)圖,能夠根據(jù)輸入的數(shù)據(jù)將溫度計(jì)里面的紅色圖片拉伸。為了達(dá)到這個(gè)效果,使用了iOS5的函數(shù):resizableImageCapInsets:(UIEdgeInsets)Insets。

最近終于申請(qǐng)到蘋果開發(fā)者賬號(hào)!搞的好煩啊!給大家?guī)Ц@耍?/p>

想真機(jī)調(diào)試,上架應(yīng)用,將IPA打包給朋友用,或者申請(qǐng)開發(fā)者賬號(hào)的請(qǐng)聯(lián)系我!

真機(jī)調(diào)試有99個(gè)限制!

其中Insets這個(gè)參數(shù)的格式是(top,left,bottom,right),從上、左、下、右分別在圖片上畫了一道線,這樣就給一個(gè)圖片加了一個(gè)框。只有在框里面的部分才會(huì)被拉伸,而框外面的部分則不會(huì)改變。比如(20,5,10,5),意思是下圖矩形里面的部分可以被拉伸,而其余部分不變。

據(jù)說stretchableImageWithLeftCapWidth:topCapHeight這個(gè)函數(shù)也能夠?qū)崿F(xiàn),但是在iOS5里面建議不要使用這個(gè)函數(shù)。效果如下圖:

當(dāng)修改了數(shù)據(jù)之后,變成這樣:

下面來看如何實(shí)現(xiàn)。

溫度計(jì)共由三張圖組成:

背景圖ThermometerBackground.png:

刻度圖ThermometerCalibration:

里面的溶液Calibration:

首先將背景圖加入superview中,再將刻度圖和溶液圖加入背景圖中:(為簡(jiǎn)化起見,一些不必要的代碼已經(jīng)省略)

  1. //將背景圖加入superview  
  2. UIImageView *thermometerBackground = [[UIImageView alloc] initWithFrame:THERMOMETER_FRAME];  
  3. [thermometerBackground setImage:[UIImage imageNamed:@"ThermometerBackground.png"]];  
  4. [self.view addSubview:self.thermometerBackground];  
  5. //將溶液圖加入背景圖  
  6. UIImageView *thermometer = [[UIImageView alloc]init];  
  7. [self.thermometerBackground addSubview:self.thermometer];  
  8. //將刻度圖加入背景圖  
  9. UIImageView *thermometerCalibration = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ThermometerCalibration.png"]];  
  10. [self.thermometerCalibration setFrame:CGRectMake(0, 10, thermometerBackground.frame.size.width, thermometerCalibration.image.size.height*thermometerBackground.frame.size.width/thermometerCalibration.frame.size.width)]; 
  1. [self.thermometerBackground addSubview:thermometerCalibration];  

然后,根據(jù)度數(shù)生成對(duì)應(yīng)高度的image

  1. UIImage* image = [UIImage imageNamed:@"Thermometer.png"];  
  2. UIEdgeInsets insets = UIEdgeInsetsMake(20, 0, 25, 0);  
  3. image = [image resizableImageWithCapInsets:insets];  
  4. int top = 10.00+(38.00-temperature)*20.00;  
  5. [self.thermometer setFrame:CGRectMake(0, top, self.thermometerBackground.frame.size.width, self.thermometerBackground.frame.size.height-top)];  
  1. [self.thermometer setImage:image];  

在這里,top這個(gè)變量就代表了根據(jù)度數(shù)計(jì)算出的溶液的高度。

這樣,當(dāng)改變溫度temperature的大小時(shí),只要在viewWillAppear里調(diào)用這段代碼,就能夠動(dòng)態(tài)生成溫度計(jì)圖片了。

責(zé)任編輯:閆佳明 來源: apkbus
相關(guān)推薦

2013-08-21 11:31:21

iPhone圖片方法

2021-12-29 06:24:16

AI審稿人工智能

2015-09-09 11:08:48

qq空間可拉伸頭部

2023-04-27 08:42:50

效果

2015-08-10 09:50:21

ios圖片文本

2015-01-20 17:15:55

iOS源碼滾動(dòng)視圖

2018-05-16 07:41:29

圖片代碼資源

2010-07-26 15:12:20

坐標(biāo)變換

2022-05-26 00:06:19

CSSFirefoxElectron

2011-05-12 11:28:20

按比例縮放

2023-04-25 17:24:31

veImageXiOSSDK

2011-09-19 15:42:33

TwitteriOS5

2013-07-29 11:19:16

iOS開發(fā)iOS開發(fā)學(xué)習(xí)FMDB更新二進(jìn)制圖片

2013-10-16 16:58:17

iOS優(yōu)化緩存優(yōu)化

2011-06-02 10:12:54

TwitteriOS 5蘋果

2015-04-23 10:15:53

AndroidiOS圖片

2015-04-23 10:52:53

AndroidiOS圖片

2011-08-18 17:20:21

IOS開發(fā)TableView圖片

2011-10-13 15:59:55

2018-03-15 15:09:29

iOS圖片標(biāo)記蘋果
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)