码迷,mamicode.com
首页 > 移动开发 > 详细

iOS App 瘦身法之图片

时间:2015-02-28 10:15:31      阅读:237      评论:0      收藏:0      [点我收藏+]

标签: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 瘦身法之图片

标签:ios

原文地址:http://blog.csdn.net/jxncwzb/article/details/43982605

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!