iOS中展開圖片的幾種方式

來源:互聯網
上載者:User

標籤:佔用   height   img   resizable   custom   參數   var   state   number   

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


按鈕背景圖片.png

我們通過代碼將這張圖片設定為按鈕的背景圖片,假如我們將建立好的按鈕的寬高設定為:(W=200, H=50)代碼如下:

////  ViewController.m//  iOS圖片展開總結//// 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]; // 建立一個按鈕 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // 設定按鈕的frame btn.frame = CGRectMake(100, 300, 200, 50); // 載入圖片 UIImage *image = [UIImage imageNamed:@"chat_send_nor"]; // 設定按鈕的背景圖片 [btn setBackgroundImage:image forState:UIControlStateNormal]; // 將按鈕添加到控制器的view [self.view addSubview:btn];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end

這是你發現啟動並執行結果完全出乎你的意料(搓的無極限),


運行1.png

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

利用蘋果提供的API來展開圖片(目前發現的有四種):
一、 方式一(iOS5之前):
如:設定topCapHeight、leftCapWidth、bottomCapHeight、lerightCapWidth,圖中的黑色地區就是圖片展開的範圍,也就是說邊上的不會被展開.
通過下面的方法我們可以設定:

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

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

// 系統會自動計算rightCapWidthrightCapWidth = image.width - leftCapWidth - 1;  // 系統會自動計算bottomCapHeightbottomCapHeight = image.height - topCapHeight - 1

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

    // 載入圖片    UIImage *image = [UIImage imageNamed:@"chat_send_nor"];    // 設定左邊端蓋寬度 NSInteger leftCapWidth = image.size.width * 0.5; // 設定上邊端蓋高度 NSInteger topCapHeight = image.size.height * 0.5; UIImage *newImage = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight]; // 設定按鈕的背景圖片 [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就是用來設定上端蓋、左端蓋、下端蓋、右端蓋的尺寸(逆時針方向);

具體代碼如下:

    // 載入圖片    UIImage *image = [UIImage imageNamed:@"chat_send_nor"];    // 設定端蓋的值 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]; // 設定按鈕的背景圖片 [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參數

typedef NS_ENUM(NSInteger, UIImageResizingMode) {    UIImageResizingModeTile, // 平鋪模式,通過重複顯示UIEdgeInsets指定的矩形地區來填充圖片 UIImageResizingModeStretch, // 展開模式,通過展開UIEdgeInsets指定的矩形地區來填充圖片};

具體代碼如下:

    // 載入圖片    UIImage *image = [UIImage imageNamed:@"chat_send_nor"];    // 設定端蓋的值 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); // 設定展開的模式 UIImageResizingMode mode = UIImageResizingModeStretch; // 展開圖片 UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode]; // 設定按鈕的背景圖片 [btn setBackgroundImage:newImage forState:UIControlStateNormal];

運行效果:


運行4.png

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


設定slicing屬性.png

 


設定後.png

是不是So easy~~

運行效果:


運行效果5.png

備忘:上面所有通過代碼來展開圖片的方法都是返回一個展開後的新圖片.



文/YotrolZ(簡書作者)
原文連結:http://www.jianshu.com/p/c9cbbdaa9b02
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。

iOS中展開圖片的幾種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.