iOS App 瘦身法之图片

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%

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。