按鈕的背景圖需要跟隨我們按鈕的大小改變而改變但是又不能失真,所以需要我們對圖片進行處理
+(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;
}
功能雖小,希望在項目中可以用的到。