[IPhone开发]ios图片局部平铺

2 篇文章 0 订阅
2 篇文章 0 订阅

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];




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值