Using the Resizableimagewithcapinsets method to implement a scalable picture

Source: Internet
Author: User

Previously introduced through the Stretchableimagewithleftcapwidth:topcapheight: method to achieve a scalable picture;

See this essay: Using the Stretchableimagewithleftcapwidth method to achieve a scalable picture

A new UIImage object instance method is provided in IOS5: Resizableimagewithcapinsets: You can convert a picture to a scalable picture that is offset by an offset value (the picture content within the offset value will not be stretched or compressed).

Stretchableimagewithleftcapwidth:topcapheight: Mode (this method has expired, recommended with the following resizableimagewithcapinsets: method);

Resizableimagewithcapinsets: The pattern can also achieve similar image gradient effect, more flexible control.

The effect is as follows:

ViewController.h

1 #import <UIKit/UIKit.h>23@interface  viewcontroller: Uiviewcontroller4@end

Viewcontroller.m

1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4- (void) Layoutui;5 @end6 7 @implementationViewcontroller8 #defineKwidthofbutton 160.09 #defineKheightofbutton 49.0Ten  One- (void) Viewdidload { A [Super Viewdidload]; -      - [self layoutui]; the } -  -- (void) didreceivememorywarning { - [Super didreceivememorywarning]; +     //Dispose of any resources the can be recreated. - } +  A- (void) Layoutui { at     //Normal Mode -UIImage *img = [UIImage imagenamed:@"BlueButton"];//size:39*39 -UIButton *btnnormal = [[UIButton alloc] Initwithframe:cgrectmake (20.0,40.0, Kwidthofbutton, Kheightofbutton)]; -[Btnnormal Settitle:@"Btnnormal"Forstate:uicontrolstatenormal]; - [Btnnormal setbackgroundimage:img forstate:uicontrolstatenormal]; - [Self.view Addsubview:btnnormal]; in      -     //stretchableimagewithleftcapwidth:topcapheight: Mode (this method has expired, recommended with the following resizableimagewithcapinsets: method) toUIImage *imgstretchable = [img stretchableimagewithleftcapwidth:16.0Topcapheight:16.0]; +UIButton *btnstretchable = [[UIButton alloc] Initwithframe:cgrectmake (20.0,50.0+Kheightofbutton, Kwidthofbutton, Kheightofbutton)]; -[Btnstretchable Settitle:@"btnstretchable"Forstate:uicontrolstatenormal]; the [btnstretchable setbackgroundimage:imgstretchable forstate:uicontrolstatenormal]; * [Self.view addsubview:btnstretchable]; $     Panax Notoginseng     //resizableimagewithcapinsets: mode; compared to stretchableimagewithleftcapwidth:topcapheight: mode, Resizableimagewithcapinsets: Mode can also achieve similar image gradient effect, more flexible control -UIImage *imgresizable = [img Resizableimagewithcapinsets:uiedgeinsetsmake (30.0,16.0,9.0,16.0)]; theUIButton *btnresizable = [[UIButton alloc] Initwithframe:cgrectmake (20.0,60.0+ (kheightofbutton*2), Kwidthofbutton, Kheightofbutton)]; +[Btnresizable Settitle:@"btnresizable"Forstate:uicontrolstatenormal]; A [btnresizable setbackgroundimage:imgresizable forstate:uicontrolstatenormal]; the [Self.view addsubview:btnresizable]; + } -  $ @end

Using the Resizableimagewithcapinsets method to implement a scalable picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.