现在的位置: 首页 > 综合 > 正文

invert Image

2013年12月08日 ⁄ 综合 ⁄ 共 1160字 ⁄ 字号 评论关闭

@implementation UIImage(OverlayColor)

- (UIImage *)imageWithOverlayColor:(UIColor *)color

{        

    CGRect rect = CGRectMake(0.0f,
0.0f, self.size.width,
self.size.height);

    

    if (UIGraphicsBeginImageContextWithOptions) {

        CGFloat imageScale =
1.0f
;

        if ([self
respondsToSelector
:@selector(scale)])

            imageScale = self.scale;

        UIGraphicsBeginImageContextWithOptions(self.size,
NO, imageScale);

    }

    else {

        UIGraphicsBeginImageContext(self.size);

    }

    

    [self drawInRect:rect];

    

    CGContextRef context =
UIGraphicsGetCurrentContext();

    CGContextSetBlendMode(context,
kCGBlendModeSourceIn);

    

    CGContextSetFillColorWithColor(context, color.CGColor);

    CGContextFillRect(context, rect);

    

    UIImage *outImage =
UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return outImage;

}

+ (UIImage *)invertImageNamed:(NSString *)name {

    

    UIColor *offWhite = [UIColor
colorWithRed :( 245.f/255.f

                                        green :( 245.f/255.f

                                         blue :( 245.f/255.f

                                        alpha:1.f];

    

    // offWhite looks nice on a scroll view background, however, 

    //  a suitable alternative is [UIColor whiteColor]

    return [[UIImage
imageNamed:name] imageWithOverlayColor:offWhite];

}

抱歉!评论已关闭.