標籤:ios
1、純色圖 顏色不同 用一張圖
瘦身代碼
- (UIImage *)imageColorDraw:(UIColor *)color{ CGSize size = self.size; CGFloat scale = [OSConfigService sharedInstance].screenScale; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, size.width * scale, size.height*scale, 8, 4 * size.width * scale, colorSpace,CG_IMAGE_ALPHA_PREMULTIPLIED_LAST); CGContextSetFillColorWithColor(context, color.CGColor); CGMutablePathRef path = CGPathCreateMutable(); CGRect rect = CGRectMake(0, 0 ,size.width * scale, size.height * scale); CGContextClipToMask(context, rect, self.CGImage); CGPathAddRect(path, NULL, rect); CGContextAddPath(context, path); CGContextFillPath(context); CGPathRelease(path); CGContextStrokePath(context); CGImageRef imageRef = CGBitmapContextCreateImage(context); UIImage *newImage = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp]; CGImageRelease(imageRef); CGColorSpaceRelease(colorSpace); CGContextRelease(context); return newImage;} rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; [rightButton setImage:[UIImage imageNamed:@"shelf_navigationbar_search.png"] forState:UIControlStateNormal]; [rightButton setImage:[[UIImage imageNamed:@"shelf_navigationbar_search.png"] imageColorDraw:RGB(245, 173, 156)] forState:UIControlStateHighlighted]; [rightButton addTarget:self action:@selector(onRightButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [rightButton sizeToFit]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton]; return backButton;
2、圖片控制項大小不一,用一張預設圖
瘦身代碼
self.contentMode = UIViewContentModeCenter; self.backgroundColor = [UIColor colorWithHex:0xf8f8f8]; self.image = [UIImage imageNamed:@"defaultImage.png"]; __weak typeof(self) weakSelf = self; [self setImageWithURLString:URLString block:^(UIImage *image) { if (image) { weakSelf.contentMode = UIViewContentModeScaleToFill; weakSelf.backgroundColor = [UIColor whiteColor]; weakSelf.image = image; } }];
3、Button按下換背景 不用圖片
瘦身代碼
#import "TBRBaseButton.h"@interface TBRBaseButton ()@property (nonatomic, STRONG) NSMutableDictionary *backgrounds;@end@implementation TBRBaseButton- (NSMutableDictionary *)backgrounds{ if (_backgrounds == nil) { _backgrounds = [NSMutableDictionary dictionary]; } return _backgrounds;}- (void)dealloc{ RELEASE(_backgrounds);}- (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state{ [self.backgrounds setObject:color forKey:[NSNumber numberWithInt:state]]; if (!self.backgroundColor || state == UIControlStateNormal) { self.backgroundColor = color; }}- (void)setBackgroundToColor:(NSNumber *)key{ UIColor *background = [self.backgrounds objectForKey:key]; if (KIND_OF_CLASSE(background, UIColor)) { [UIView animateWithDuration:0.1f animations:^{ self.backgroundColor = background; }]; }}- (void)setEnabled:(BOOL)theEnabled{ [super setEnabled:theEnabled]; if (!theEnabled) { [self setBackgroundToColor:[NSNumber numberWithInt:UIControlStateDisabled]]; } else { [self setBackgroundToColor:[NSNumber numberWithInt:UIControlStateNormal]]; }}- (void)setSelected:(BOOL)theSelected{ [super setSelected:theSelected]; if (theSelected) { [self setBackgroundToColor:[NSNumber numberWithInt:UIControlStateSelected]]; } else { [self setBackgroundToColor:[NSNumber numberWithInt:UIControlStateNormal]]; }}- (void)setHighlighted:(BOOL)theHighlighted{ [super setHighlighted:theHighlighted]; if (theHighlighted) { [self setBackgroundToColor:[NSNumber numberWithInt:UIControlStateHighlighted]]; } else { [self setBackgroundToColor:[NSNumber numberWithInt:UIControlStateNormal]]; }}@end
4、用 ImageAlpha 壓縮包裡的圖片;可壓縮 20%~50%
iOS App 瘦身法之圖片