iphone图片拉伸相关

 

关于让图片在指定位置拉伸,有一个函数

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

 (支持ios2--ios4)

这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是左边不拉伸区域的宽度,第二个参数是上面不拉伸的高度。根据设置的宽度和高度,将接下来的一个像素进行左右扩展和上下拉伸。

注意:可拉伸的范围都是距离leftCapWidth后的1竖排像素,和距离topCapHeight后的1横排像素。

以上函数在apple官方文档中定义为(Deprecated in iOS 5.0),虽经验证在5.0.1上可以使用并不报错,不过推荐在IOS5上使用以下函数:

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

 (仅支持ios5)

 

相关代码:

UIImage *bg = [[UIImage imageNamed:@"camera.png"]stretchableImageWithLeftCapWidth:30 topCapHeight:0];

UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];

bgView.frame = CGRectMake(100, 200, 160, 60);

[self.view addSubview:bgView];

[bg release];

[bgView release];

 

UIImage *bg = [[UIImageimageNamed:@"camera.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];

UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];

bgView.frame = CGRectMake(100, 200, 160, 60);

[self.view addSubview:bgView];

[bg release];

[bgView release];

 

参考网页https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UIImage/stretchableImageWithLeftCapWidth:topCapHeight:

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/resizableImageWithCapInsets:


    --yuzhang2

posted @ 2012-03-17 14:46  iPhone Dev  阅读(1665)  评论(0编辑  收藏  举报