按钮的背景图需要跟随我们按钮的大小改变而改变但是又不能失真,所以需要我们对图片进行处理
+(UIImage *)resizableImage:(NSString *)imageName {
UIImage *image = [UIImage imageNamed:imageName];
CGFloat w = image.size.width* 0.5;
CGFloat h = image.size.height * 0.5;
return [image resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w) resizingMode:UIImageResizingModeStretch];
}
另外还有个制作纯色背景图的效果,只需传入我们需要的颜色便可
+ (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
功能虽小,希望在项目中可以用的到。