ios拓展18-圖片純色渲染分類

開(kāi)發(fā)中,如果對(duì)于同一套圖,需要設(shè)置不同顏色,可以通過(guò)程序自行渲染

1.方法聲明
#import <UIKit/UIKit.h>

@interface UIImage (Tint)

/**
 *  為圖片填充自定義顏色
 *
 *  @param tintColor 需要填充的顏色
 *
 *  @return 填充后的顏色
 */
- (UIImage *)imageWithTintColor:(UIColor *)tintColor;

/**
 *  為圖片填充自定義顏色
 *
 *  @param tintColor 需要填充的顏色
 *
 *  @return 填充后的顏色
 */
- (UIImage *)imageWithGradientTintColor:(UIColor *)tintColor;

@end
2.方法實(shí)現(xiàn)
#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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,922評(píng)論 25 709
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,536評(píng)論 19 139
  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,603評(píng)論 30 472
  • 雁過(guò)魚驚傳尺素,止不住,相思傾與付。 我去看她的時(shí)候,她靠在窗前,不斷的唱著這句詞,青絲散了一地,穿了她新婚時(shí)穿的...
    招搖山主閱讀 487評(píng)論 0 0
  • 如果時(shí)間在此刻停止 我是否該去問(wèn)她為何離去 又是否會(huì)明白自己為何留下 她說(shuō)是風(fēng)兒要帶她去旅行 她說(shuō)那是她夢(mèng)里的地方...
    觴呃閱讀 150評(píng)論 0 0

友情鏈接更多精彩內(nèi)容