1. 程式人生 > >iOS 修改同一套圖的顏色

iOS 修改同一套圖的顏色

第一種:很簡單,但是隻能更改圖片中線條的顏色,不能更改填充的顏色
步驟:1 在圖片管理器中選中圖片
2:點選圖片,選中如圖格式
3:在程式碼中更改uiimageView顏色:

      imgV.tintColor = [UIColor blackColor];

第二種:可以將裡面的內容的顏色為想要的顏色
1 建立一個類別
注意紅線選中地方

2 在建立的類別寫如下程式碼
@interface UIImage (UIImage_Tint)

- (UIImage *) imageWithTintColor:(UIColor *)tintColor;
- (UIImage
*) imageWithGradientTintColor:(UIColor *)tintColor; @end

3 在.m檔案中

 #import "UIImage+UIImage_Tint.h"

@implementation UIImage (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); //Draw the tinted image in context [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; }