ios 확장18 - 그림 순수한 색 렌더링 분류

1446 단어
개발 중 같은 세트의 그림에 대해 다른 색을 설정해야 한다면 프로그램을 통해 스스로 렌더링할 수 있다
1. 방법 설명
#import 

@interface UIImage (Tint)

/**
 *            
 *
 *  @param tintColor        
 *
 *  @return       
 */
- (UIImage *)imageWithTintColor:(UIColor *)tintColor;

/**
 *            
 *
 *  @param tintColor        
 *
 *  @return       
 */
- (UIImage *)imageWithGradientTintColor:(UIColor *)tintColor;

@end

2. 방법 실현
#import "UIImage+Tint.h"

@implementation UIImage (Tint)

- (UIImage *) imageWithTintColor:(UIColor *)tintColor
{
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeDestinationIn];
}

- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor
{
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay];
}

- (UIImage *) imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode
{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
    [tintColor setFill];
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
    UIRectFill(bounds);
    
    [self drawInRect:bounds blendMode:blendMode alpha:1.0f];
    
    if (blendMode != kCGBlendModeDestinationIn) {
        [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
    }
    
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return tintedImage;
}

@end

좋은 웹페이지 즐겨찾기