Simple image stretching and simple image stretching
The initial image size is 25*28 pixels.
Now we try to create two buttons on the Root
UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom];
Button. frame = CGRectMake (50,100, 25, 28 );
[Button setBackgroundImage: [UIImage imageNamed: @ "btn.png"] forState: UIControlStateNormal];
[Self. view addSubview: button];
UIButton * button1 = [UIButton buttonWithType: UIButtonTypeCustom];
Button1.frame = CGRectMake (50,200,200, 28 );
UIImage * image = [UIImage imageNamed: @ "btn.png"];
[Button1 setBackgroundImage: image forState: UIControlStateNormal];
[Self. view addSubview: button1];
Image effect before stretching
In iOS 5.0, there is a new way for UIImage to handle image stretching problems.
-(UIImage *) resizableImageWithCapInsets :( UIEdgeInsets) capInsets
CGFloat left = 12; // left end width
CGFloat right = 12; // right side width
UIEdgeInsets insets = UIEdgeInsetsMake (0, left, 0, right );
// Assign a value after scaling
UIImage * new = [image resizableImageWithCapInsets: insets];
[Button1 setBackgroundImage: new forState: UIControlStateNormal];
[Self. view addSubview: button1];
In iOS6.0, UIImage provides another method for processing image stretching.
-(UIImage *) resizableImageWithCapInsets :( UIEdgeInsets) capInsets resizingMode :( UIImageResizingMode) resizingMode
Compared with iOS5.0, only one UIImageResizingMode parameter is added to specify the stretch mode:
UIImageResizingModeStretch: stretch mode. Fill the image by stretching the area of the rectangle specified by UIEdgeInsets.
UIImageResizingModeTile: tiled mode. The image is filled by repeatedly displaying the rectangle area specified by UIEdgeInsets.
CGFloat left = 12; // left end width
CGFloat right = 12; // right side width
UIEdgeInsets insets = UIEdgeInsetsMake (0, left, 0, right );
// Assign a value after scaling
UIImage * new = [image resizableImageWithCapInsets: insets resizingMode: UIImageResizingModeStretch];
[Button1 setBackgroundImage: new forState: UIControlStateNormal];
[Self. view addSubview: button1];
The following figure shows the stretched image.