注意:可拉伸的範圍都是距離leftCapWidth後的1豎排像素,和距離topCapHeight後的1橫排像素。atom
參數的意義是,若是參數指定10,5。那麼,圖片左邊10個像素,上邊5個像素。不會被拉伸,x座標爲11和一個像素會被橫向複製,y座標爲6的一個像素會被縱向複製。spa
- (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的拓展使用
code
+ (UIImage *)resizedImage:(NSString *)name { UIImage *image = [UIImage imageWithName:name]; return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5]; }