UIImage的方法和技巧汇总

图片拉伸


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

参数的意义是,如果参数指定10,5。那么,图片左边10个像素,上边5个像素。不会被拉伸,x坐标为11和一个像素会被横向复制,y坐标为6的一个像素会被纵向复制。


- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
@property(nonatomic,readonly) NSInteger leftCapWidth;   // default is 0. if non-zero, horiz. stretchable. right cap is calculated as width - leftCapWidth - 1
@property(nonatomic,readonly) NSInteger topCapHeight;   // default is 0. if non-zero, vert. stretchable. bottom cap is calculated as height - topCapWidth - 1



UIImage *img=[UIImage imageNamed:@"bubbleSelf.png"];
    img=[img stretchableImageWithLeftCapWidth:15 topCapHeight:12];
    UIImageView *imgView=[[UIImageView alloc]initWithImage:img];
    [imgView setFrame:CGRectMake(10, 10, 200, 200)];
    [self. view addSubview:imgView];


UIImage的方法和技巧汇总


封装的一个拉伸方法,可以作为UIImage的拓展使用

+ (UIImage *)resizedImage:(NSString *)name
{
    UIImage *image = [UIImage imageWithName:name];
    return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
}




你可能感兴趣的:(UIImage的方法和技巧汇总)