Apple ios5.0之前,提供了方法:
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
ios5.0以后,提供了方法:
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
官网的描述是:
stretchableImageWithLeftCapWidth:topCapHeight:
Creates and returns a new image object with the specified cap values. (Deprecated in iOS 5.0. Deprecated. Use the resizableImageWithCapInsets: instead, specifying cap insets such that the interior is a 1x1 area.)
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
Parameters
leftCapWidth
The value to use for the left cap width. Specify 0 if you want the entire image to be horizontally stretchable. For a discussion of how a non-zero value affects the image, see the leftCapWidth property.
topCapHeight
The value to use for the top cap width. Specify 0 if you want the entire image to be vertically stretchable. For a discussion of how a non-zero value affects the image, see the topCapHeight property.
Return Value
A new image object with the specified cap values.
resizableImageWithCapInsets:
Creates and returns a new image object with the specified cap insets.
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
Parameters
capInsets
The values to use for the cap insets.
Return Value
A new image object with the specified cap insets.
Discussion
You use this method to add cap insets to an image or to change the existing cap insets of an image. In both cases, you get back a new image and the original image remains untouched.
During scaling or resizing of the image, areas covered by a cap are not scaled or resized. Instead, the pixel area not covered by the cap in each direction is tiled, left-to-right and top-to-bottom, to resize the image. This technique is often used to create variable-width buttons, which retain the same rounded corners but whose center region grows or shrinks as needed. For best performance, use a tiled area that is a 1x1 pixel area in size.
Availability
Available in iOS 5.0 and later.
Declared In
UIImage.h
所以,在使用背景图片局部平铺时,需要判断操作系统的版本,判断系统版本的方法是:
//设定tableview背景
UIImage *tableViewBg = nil;
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 5.0)
tableViewBg = [[UIImage imageNamed:@"01imgagebackground.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(30, 0, 0, 0)];
else
tableViewBg = [[UIImage imageNamed:@"01imgagebackground.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:30];