标签: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;
}
}];
#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
原文地址:http://blog.csdn.net/jxncwzb/article/details/43982605