iOS中拉伸圖片的幾種方式

版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上原文出處鏈接和本聲明。
本文鏈接:https://yotrolz.com/posts/5fe4e0ec/


假如下面的一張圖片,是用來做按鈕的背景圖片的,原始尺寸是(128 * 112)

按鈕背景圖片.png

我們通過代碼將這張圖片設(shè)置為按鈕的背景圖片,假如我們將創(chuàng)建好的按鈕的寬高設(shè)置為:(W=200, H=50)代碼如下:

//
//  ViewController.m
//  iOS圖片拉伸總結(jié)
//
//  Copyright (c) 2015年 YotrolZ. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 創(chuàng)建一個按鈕
    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)運行的結(jié)果完全出乎你的意料(搓的無極限),如圖:


運行效果圖1.png

原因分析:是將原是尺寸為W=128 * H=112的圖片拉伸成了W=200, H=50;
解決方案:
1.找美工MM重做一張較大的圖片,這樣的話就會出現(xiàn)軟件包將來會變大,占用空間更大;如果我們要經(jīng)常修改按鈕的frame,你是想讓MM殺你的節(jié)奏~~,顯然不可行;
2.蘋果為我們提供了關(guān)于圖片拉伸的API,我們可以直接利用代碼實現(xiàn),是不是很牛X;

利用蘋果提供的API來拉伸圖片(目前發(fā)現(xiàn)的有四種):
一、 方式一(iOS5之前):
如下圖:設(shè)置topCapHeight、leftCapWidth、bottomCapHeight、lerightCapWidth,圖中的黑色區(qū)域就是圖片拉伸的范圍,也就是說邊上的不會被拉伸.
通過下面的方法我們可以設(shè)置:

// 官方API說明
// - stretchableImageWithLeftCapWidth:topCapHeight:(iOS 5.0)
// Creates and returns a new image object with the specified cap values.

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

// 系統(tǒng)會自動計算rightCapWidth
rightCapWidth = image.width - leftCapWidth - 1;  
  
// 系統(tǒng)會自動計算bottomCapHeight
bottomCapHeight = image.height - topCapHeight - 1 

這樣一來,其實我們圖片的可拉伸范圍只有1 * 1,所以再怎么拉伸都不會影響圖片的外觀;
具體代碼如下:

    // 加載圖片
    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];

運行效果:


運行效果圖2.png

方式二:(iOS5)
利用下面的方法:

// 官方API說明
- (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;

說明:UIEdgeInsets中的CGFloat top, left, bottom, right就是用來設(shè)置上端蓋、左端蓋、下端蓋、右端蓋的尺寸(逆時針方向);

具體代碼如下:

    // 加載圖片
    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];

運行效果:


運行效果圖3.png

方式三:(iOS6)
利用下面的方法:

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); 
// the interior is resized according to the resizingMode

說明:相比iOS5中的方法多了一個resizingMode參數(shù)

typedef NS_ENUM(NSInteger, UIImageResizingMode) {
    UIImageResizingModeTile, // 平鋪模式,通過重復(fù)顯示UIEdgeInsets指定的矩形區(qū)域來填充圖片
    UIImageResizingModeStretch, // 拉伸模式,通過拉伸UIEdgeInsets指定的矩形區(qū)域來填充圖片
};

具體代碼如下:

    // 加載圖片
    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];

運行效果:

運行效果圖4.png

方式4:(最簡單的一種方式)


設(shè)置slicing屬性.png

設(shè)置后.png

是不是So easy~~

運行效果:

運行效果5.png

備注:上面所有通過代碼來拉伸圖片的方法都是返回一個拉伸后的新圖片.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,623評論 4 61
  • 縱觀移動市場,一款移動app,要想長期在移動市場立足,最起碼要包含以下幾個要素:實用的功能、極強的用戶體驗、華麗簡...
    木馬不在轉(zhuǎn)閱讀 592評論 0 0
  • 讀書有三種類型,一是享受趣味,二是獲取新知,三是改變?nèi)松?。這本書講述的就是如何通過讀書改變?nèi)松?。讀書過程分為三個階...
    拾憶芳華閱讀 330評論 0 2
  • 對于我 想喝酒 可以有 酒店 餐館 大排檔 還有獨居的 小屋 紅星二鍋頭 茅臺 或是南非的紅酒 還有 比利時啤...
    沿海而行閱讀 281評論 0 0
  • 湖邊風(fēng)兒吹 吹的頭發(fā)癢 臉蛋冰冰涼 前方路漫長 腳步不可擋 快樂奔健康 歌兒輕輕唱 抬頭望月亮 默默把你想 想你在...
    開心果_6e66閱讀 186評論 0 0

友情鏈接更多精彩內(nèi)容