iOS App 瘦身法之圖片

來源:互聯網
上載者:User

標籤: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 瘦身法之圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.