iOS圖片拉伸的4種方法
假如下面的一張圖片,是用來(lái)做按鈕的背景圖片的,原始尺寸是(128 * 112)

按鈕背景圖片.png
我們通過(guò)代碼將這張圖片設(shè)置為按鈕的背景圖片,假如我們將創(chuàng)建好的按鈕的寬高設(shè)置為:(W=200, H=50)代碼如下:
//
// ViewController.m
// iOS圖片拉伸總結(jié)
//
// Created by Sunshine on 15/6/29.
// Copyright (c) 2015年 YotrolZ. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建一個(gè)按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// 設(shè)置按鈕的frame
btn.frame = CGRectMake(100, 300, 200, 50);
// 加載圖片
UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
// 設(shè)置按鈕的背景圖片
[btn setBackgroundImage:image forState:UIControlStateNormal];
// 將按鈕添加到控制器的view
[self.view addSubview:btn];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
這是你發(fā)現(xiàn)運(yùn)行的結(jié)果完全出乎你的意料(搓的無(wú)極限),如圖:

運(yùn)行效果圖1.png
原因分析:是將原是尺寸為W=128 * H=112的圖片拉伸成了W=200, H=50;
解決方案:
1.找美工MM重做一張較大的圖片,這樣的話就會(huì)出現(xiàn)軟件包將來(lái)會(huì)變大,占用空間更大;如果我們要經(jīng)常修改按鈕的frame,你是想讓MM殺你的節(jié)奏~~,顯然不可行;
2.蘋(píng)果為我們提供了關(guān)于圖片拉伸的API,我們可以直接利用代碼實(shí)現(xiàn),是不是很牛X;
利用蘋(píng)果提供的API來(lái)拉伸圖片(目前發(fā)現(xiàn)的有四種):
方式一(iOS5之前):
如下圖:設(shè)置topCapHeight、leftCapWidth、bottomCapHeight、lerightCapWidth,圖中的黑色區(qū)域就是圖片拉伸的范圍,也就是說(shuō)邊上的不會(huì)被拉伸.
通過(guò)下面的方法我們可以設(shè)置:
// 官方API說(shuō)明
// - stretchableImageWithLeftCapWidth:topCapHeight:(iOS 5.0)
// Creates and returns a new image object with the specified cap values.
說(shuō)明:這個(gè)方法只有2個(gè)參數(shù),leftCapWidth代表左端蓋寬度,topCapHeight代表上端蓋高度。系統(tǒng)會(huì)自動(dòng)計(jì)算出右端蓋寬度rightCapWidth和底端蓋高度bottomCapHeight,算法如下:
// 系統(tǒng)會(huì)自動(dòng)計(jì)算rightCapWidth rightCapWidth = image.width - leftCapWidth - 1; // 系統(tǒng)會(huì)自動(dòng)計(jì)算bottomCapHeight bottomCapHeight = image.height - topCapHeight - 1
這樣一來(lái),其實(shí)我們圖片的可拉伸范圍只有1 * 1,所以再怎么拉伸都不會(huì)影響圖片的外觀;
具體代碼如下:
// 加載圖片 UIImage *image = [UIImage imageNamed:@"chat_send_nor"]; // 設(shè)置左邊端蓋寬度 NSInteger leftCapWidth = image.size.width * 0.5; // 設(shè)置上邊端蓋高度 NSInteger topCapHeight = image.size.height * 0.5; UIImage *newImage = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight]; // 設(shè)置按鈕的背景圖片 [btn setBackgroundImage:newImage forState:UIControlStateNormal];
運(yùn)行效果:

運(yùn)行效果圖2.png
方式二:(iOS5)
利用下面的方法:
// 官方API說(shuō)明
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(5_0);
// create a resizable version of this image. the interior is tiled when drawn.
typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right;
// specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;
說(shuō)明:UIEdgeInsets中的CGFloat top, left, bottom, right就是用來(lái)設(shè)置上端蓋、左端蓋、下端蓋、右端蓋的尺寸(逆時(shí)針?lè)较?;
具體代碼如下:
// 加載圖片 UIImage *image = [UIImage imageNamed:@"chat_send_nor"]; // 設(shè)置端蓋的值 CGFloat top = image.size.height * 0.5; CGFloat left = image.size.width * 0.5; CGFloat bottom = image.size.height * 0.5; CGFloat right = image.size.width * 0.5; UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right); // 拉伸圖片 UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets]; // 設(shè)置按鈕的背景圖片 [btn setBackgroundImage:newImage forState:UIControlStateNormal];
運(yùn)行效果:

運(yùn)行效果圖3.png
方式三:(iOS6)
利用下面的方法:
// the interior is resized according to the resizingMode
說(shuō)明:相比iOS5中的方法多了一個(gè)resizingMode參數(shù)
typedef NS_ENUM(NSInteger, UIImageResizingMode) {
UIImageResizingModeTile, // 平鋪模式,通過(guò)重復(fù)顯示UIEdgeInsets指定的矩形區(qū)域來(lái)填充圖片
UIImageResizingModeStretch, // 拉伸模式,通過(guò)拉伸UIEdgeInsets指定的矩形區(qū)域來(lái)填充圖片
};
具體代碼如下:
// 加載圖片 UIImage *image = [UIImage imageNamed:@"chat_send_nor"]; // 設(shè)置端蓋的值 CGFloat top = image.size.height * 0.5; CGFloat left = image.size.width * 0.5; CGFloat bottom = image.size.height * 0.5; CGFloat right = image.size.width * 0.5; // 設(shè)置端蓋的值 UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right); // 設(shè)置拉伸的模式 UIImageResizingMode mode = UIImageResizingModeStretch; // 拉伸圖片 UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode]; // 設(shè)置按鈕的背景圖片 [btn setBackgroundImage:newImage forState:UIControlStateNormal];
運(yùn)行效果:

運(yùn)行效果圖4.png
方式4:(最簡(jiǎn)單的一種方式)

設(shè)置slicing屬性.png

設(shè)置后.png
是不是So easy~~
運(yùn)行效果:

運(yùn)行效果5.png
備注:上面所有通過(guò)代碼來(lái)拉伸圖片的方法都是返回一個(gè)拉伸后的新圖片。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS使用UICollectionView實(shí)現(xiàn)列表頭部拉伸效果
- iOS tableview實(shí)現(xiàn)頂部拉伸效果
- iOS實(shí)現(xiàn)頭部拉伸效果
- iOS tableView實(shí)現(xiàn)頭部拉伸并改變導(dǎo)航條漸變色
- iOS圖片實(shí)現(xiàn)可拉伸不變形的處理操作
- iOS應(yīng)用開(kāi)發(fā)中圖片的拉伸問(wèn)題解決方案
- iOS中實(shí)現(xiàn)圖片自適應(yīng)拉伸效果的方法
- iOS圖片拉伸小技巧
- iOS 解決按鈕背景圖片拉伸問(wèn)題(推薦)
- iOS tableView實(shí)現(xiàn)頂部圖片拉伸效果
相關(guān)文章
IOS 波紋進(jìn)度(waveProgress)動(dòng)畫(huà)實(shí)現(xiàn)
這篇文章主要介紹了IOS 紋進(jìn)度(waveProgress)動(dòng)畫(huà)實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-09-09
iOS中如何判斷當(dāng)前網(wǎng)絡(luò)環(huán)境是2G/3G/4G/5G/WiFi
這篇文章主要給大家介紹了關(guān)于iOS中如何判斷當(dāng)前網(wǎng)絡(luò)環(huán)境是2G/3G/4G/5G/WiFi的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
如何為Xcode添加刪除整行、復(fù)制整行及在下方新建一行快捷鍵詳解
xcode是蘋(píng)果公司向開(kāi)發(fā)人員提供的集成開(kāi)發(fā)環(huán)境,開(kāi)發(fā)者們經(jīng)常會(huì)使用到,下面這篇文章主要給大家介紹了關(guān)于如何為Xcode添加刪除整行、復(fù)制整行及在下方新建一行快捷鍵的相關(guān)資料,需要的朋友可以參考下。2018-04-04
UITableViewCell在編輯狀態(tài)下背景顏色的修改方法
這篇文章主要給大家介紹了關(guān)于UITableViewCell在編輯狀態(tài)下背景顏色的修改方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
iOS Mask屬性的詳細(xì)介紹及應(yīng)用實(shí)例
這篇文章主要介紹了iOS Mask屬性的詳細(xì)介紹的相關(guān)資料,這里對(duì)Mask的屬性進(jìn)行了詳細(xì)說(shuō)明并附簡(jiǎn)單代碼實(shí)例,幫助大家更直接學(xué)習(xí)理解,這部分知識(shí),需要的朋友可以參考下2016-11-11
淺析Objective-C中分類(lèi)Category的使用
這篇文章主要介紹了淺析Objective-C中分類(lèi)Category的使用,使用Category對(duì)類(lèi)進(jìn)行擴(kuò)展可以訪問(wèn)原始類(lèi)的實(shí)例變量,需要的朋友可以參考下2016-03-03

