iOS圖片部分拉伸

簡(jiǎn)介:有時(shí)候我們只是想把圖片部分拉伸,而不是全部拉伸,那么就要用到下面這個(gè)函數(shù),并附上實(shí)例驗(yàn)證

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
  //leftCapWidth:左邊不拉伸區(qū)域
  //topCapHeight:上面不拉伸區(qū)域

對(duì)距離leftCapWidth的1豎排像素,和具體topCapHeight的1橫排像素進(jìn)行拉伸,其它像素不拉伸!


練習(xí)一:理解拉伸點(diǎn),左邊和上面分開(kāi)設(shè)置,從效果圖來(lái)理解。
縱向拉伸

self.view.backgroundColor = [UIColor darkGrayColor];
//不設(shè)置拉伸點(diǎn),直接設(shè)置
UIImage *image1 = [UIImage imageNamed:@QQ];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 170, 170)];
imageView1.image = image1;
[self.view addSubview:imageView1];

//設(shè)置拉伸點(diǎn),對(duì)左邊和上面分開(kāi)設(shè)置,理解其拉伸效果
UIImage *image2 = [UIImage imageNamed:@QQ];
image2 = [image2 stretchableImageWithLeftCapWidth:0 topCapHeight:image2.size.height*0.5];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(190, 100, 170, 170)];
imageView2.image = image2;
[self.view addSubview:imageView2];

效果圖


橫向拉伸:

image2 = [image2 stretchableImageWithLeftCapWidth:image2.size.width*0.5 topCapHeight:0];

效果圖:



練習(xí)二:最近做到一個(gè)聊天框的聯(lián)系,對(duì)文字背景圖片的拉伸應(yīng)用,左邊的小三角我不希望拉伸,拉伸影響美觀,只拉伸右邊方框部分,那么調(diào)用此函數(shù)就可以實(shí)現(xiàn),這個(gè)較為常用。

//不設(shè)置拉伸點(diǎn),直接設(shè)置
UIImage *image1 = [UIImage imageNamed:@chat];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 80)];
imageView1.image = image1;
[self.view addSubview:imageView1];

//設(shè)置拉伸點(diǎn)
UIImage *image2 = [UIImage imageNamed:@chat];
image2 = [image2 stretchableImageWithLeftCapWidth:image2.size.width*0.5 topCapHeight:image2.size.width*0.8];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 80)];
imageView2.image = image2;
[self.view addSubview:imageView2];

效果圖:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容