使用 resizableImageWithCapInsets 方法實現可伸縮圖片

來源:互聯網
上載者:User

標籤:

之前介紹過通過 stretchableImageWithLeftCapWidth:topCapHeight: 方法來實現可伸縮圖片;

可看這篇隨筆:使用 stretchableImageWithLeftCapWidth 方法實現可伸縮圖片

 

iOS5 中提供了一個新的 UIImage 對象執行個體方法:resizableImageWithCapInsets:,可以將圖片轉換為以某一位移值為位移的可伸縮圖片(位移值內的圖片內容將不被展開或壓縮)。

stretchableImageWithLeftCapWidth:topCapHeight: 模式(此方法已經到期,推薦用下面的resizableImageWithCapInsets: 方法);

resizableImageWithCapInsets: 模式還能實作類別似圖片漸層的效果,更靈活控制。

 

效果如下:

ViewController.h

1 #import <UIKit/UIKit.h>2 3 @interface ViewController : UIViewController4 @end

ViewController.m

 1 #import "ViewController.h" 2  3 @interface ViewController () 4 - (void)layoutUI; 5 @end 6  7 @implementation ViewController 8 #define kWidthOfButton 160.0 9 #define kHeightOfButton 49.010 11 - (void)viewDidLoad {12     [super viewDidLoad];13     14     [self layoutUI];15 }16 17 - (void)didReceiveMemoryWarning {18     [super didReceiveMemoryWarning];19     // Dispose of any resources that can be recreated.20 }21 22 - (void)layoutUI {23     //普通模式24     UIImage *img = [UIImage imageNamed:@"blueButton"]; //size: 39*3925     UIButton *btnNormal = [[UIButton alloc] initWithFrame:CGRectMake(20.0, 40.0, kWidthOfButton, kHeightOfButton)];26     [btnNormal setTitle:@"btnNormal" forState:UIControlStateNormal];27     [btnNormal setBackgroundImage:img forState:UIControlStateNormal];28     [self.view addSubview:btnNormal];29     30     //stretchableImageWithLeftCapWidth:topCapHeight:模式(此方法已經到期,推薦用下面的resizableImageWithCapInsets:方法)31     UIImage *imgStretchable = [img stretchableImageWithLeftCapWidth:16.0 topCapHeight:16.0];32     UIButton *btnStretchable = [[UIButton alloc] initWithFrame:CGRectMake(20.0, 50.0+kHeightOfButton, kWidthOfButton, kHeightOfButton)];33     [btnStretchable setTitle:@"btnStretchable" forState:UIControlStateNormal];34     [btnStretchable setBackgroundImage:imgStretchable forState:UIControlStateNormal];35     [self.view addSubview:btnStretchable];36     37     //resizableImageWithCapInsets:模式;相比stretchableImageWithLeftCapWidth:topCapHeight:模式來說,resizableImageWithCapInsets:模式還能實作類別似圖片漸層的效果,更靈活控制38     UIImage *imgResizable = [img resizableImageWithCapInsets:UIEdgeInsetsMake(30.0, 16.0, 9.0, 16.0)];39     UIButton *btnResizable = [[UIButton alloc] initWithFrame:CGRectMake(20.0, 60.0+(kHeightOfButton*2), kWidthOfButton, kHeightOfButton)];40     [btnResizable setTitle:@"btnResizable" forState:UIControlStateNormal];41     [btnResizable setBackgroundImage:imgResizable forState:UIControlStateNormal];42     [self.view addSubview:btnResizable];43 }44 45 @end

 

使用 resizableImageWithCapInsets 方法實現可伸縮圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.