17

現在の UIImage (白) の上に黒のオーバーレイを追加しようとしています。私は次のコードを使用しようとしています:

[[UIColor blackColor] set];
[image drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeOverlay alpha:1.0];

しかし、それは機能していません。 set がそこにあるはずがないと確信しています。

4

9 に答える 9

54

したがって、すべての答えを 1 つにまとめると、あらゆる種類の画像やアイコンにiOS 6至るまで完全に機能するドロップイン メソッドがここにあります。iOS 11

+ (UIImage *)filledImageFrom:(UIImage *)source withColor:(UIColor *)color{

    // begin a new image context, to draw our colored image onto with the right scale
    UIGraphicsBeginImageContextWithOptions(source.size, NO, [UIScreen mainScreen].scale);

    // get a reference to that context we created
    CGContextRef context = UIGraphicsGetCurrentContext();

    // set the fill color
    [color setFill];

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords
    CGContextTranslateCTM(context, 0, source.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetBlendMode(context, kCGBlendModeColorBurn);
    CGRect rect = CGRectMake(0, 0, source.size.width, source.size.height);
    CGContextDrawImage(context, rect, source.CGImage);

    CGContextSetBlendMode(context, kCGBlendModeSourceIn);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);

    // generate a new UIImage from the graphics context we drew onto
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return the color-burned image
    return coloredImg;
}

更新: Swift 3 バージョン

func filledImage(source: UIImage, fillColor: UIColor) -> UIImage {

    UIGraphicsBeginImageContextWithOptions(source.size, false, UIScreen.main.scale)

    let context = UIGraphicsGetCurrentContext()
    fillColor.setFill()

    context!.translateBy(x: 0, y: source.size.height)
    context!.scaleBy(x: 1.0, y: -1.0)

    context!.setBlendMode(CGBlendMode.colorBurn)
    let rect = CGRect(x: 0, y: 0, width: source.size.width, height: source.size.height)
    context!.draw(source.cgImage!, in: rect)

    context!.setBlendMode(CGBlendMode.sourceIn)
    context!.addRect(rect)
    context!.drawPath(using: CGPathDrawingMode.fill)

    let coloredImg : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return coloredImg
}
于 2014-05-08T12:00:05.820 に答える
31

コンテキストを画像マスクにクリップしてから、単色で塗りつぶします。

- (void)drawRect:(CGRect)rect
{
    CGRect bounds = [self bounds];
    [[UIColor blackColor] set];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClipToMask(context, bounds, [myImage CGImage]);
    CGContextFillRect(context, bounds);
}

注:myImageを含むインスタンス変数である必要がありますUIImage。アルファチャンネルからマスクを取得するのか、強度からマスクを取得するのかわかりませんので、両方を試してください。

于 2009-05-11T03:37:33.933 に答える
11

これに役立つチュートリアルを書きました。私のアプローチでは、必要な色の変更を加えた UIImage のコピーが提供されます。rpetrich のアプローチは優れていますが、サブクラスを作成する必要があります。私のアプローチは、必要な場所にドロップできる数行のコードです。http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage

NSString *name = @"badge.png";
UIImage *img = [UIImage imageNamed:name];

// begin a new image context, to draw our colored image onto
UIGraphicsBeginImageContext(img.size);

// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();

// set the fill color
[color setFill];

// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

// set the blend mode to color burn, and the original image
CGContextSetBlendMode(context, kCGBlendModeColorBurn);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, rect, img.CGImage);

// set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
CGContextClipToMask(context, rect, img.CGImage);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);

// generate a new UIImage from the graphics context we drew onto
UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//return the color-burned image
return coloredImg;
于 2010-09-27T17:40:28.850 に答える
9

iOS 7 以降、はるかに簡単な解決策があります。

UIImage* im = [UIImage imageNamed:@"blah"];
im = [im imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[[UIColor blackColor] setFill];
[im drawInRect:rect];
于 2014-07-05T20:42:40.250 に答える
9

この方法を見て

 + (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size;
    {
      UIImage *img = nil;

      CGRect rect = CGRectMake(0, 0, size.width, size.height);
      UIGraphicsBeginImageContext(rect.size);
      CGContextRef context = UIGraphicsGetCurrentContext();
      CGContextSetFillColorWithColor(context,
                                     color.CGColor);
      CGContextFillRect(context, rect);
      img = UIGraphicsGetImageFromCurrentImageContext();

      UIGraphicsEndImageContext();

      return img;
    }
于 2012-05-16T13:57:06.713 に答える
3

rpetrich によるソリューション (ちなみにこれは素晴らしいです - 私を大いに助けてくれます) に加えて、CGContextClipToMask 行を次のように置き換えることもできます。

    CGContextSetBlendMode(context, kCGBlendModeSourceIn); //this is the main bit!

GetCurrentContext にあるものによって色をマスクするのは、SourceIn ブレンドモードです。

于 2009-07-21T23:26:34.673 に答える
1

Swift 3.0で UIImage の拡張機能を使用する方法は次のとおりです。超シンプル。

public extension UIImage {
    func filledImage(fillColor: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)

        let context = UIGraphicsGetCurrentContext()!
        fillColor.setFill()

        context.translateBy(x: 0, y: self.size.height)
        context.scaleBy(x: 1.0, y: -1.0)

        context.setBlendMode(CGBlendMode.colorBurn)

        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context.draw(self.cgImage!, in: rect)

        context.setBlendMode(CGBlendMode.sourceIn)
        context.addRect(rect)
        context.drawPath(using: CGPathDrawingMode.fill)

        let coloredImg : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        return coloredImg
    }
}

それを実行するには、次のようにします。

let image = UIImage(named: "image_name").filledImage(fillColor: UIColor.red)
于 2016-12-05T02:55:36.810 に答える
-1

-setブリットを含まない後続の描画操作の色を設定するために使用されます。UIView最初の呼び出しとして、あなたの上に別の(空)を表示しUIImageView、その背景色を設定することをお勧めします:

myView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];

明らかに、必要な白とアルファの値を使用する必要があります。

于 2009-05-10T13:21:57.703 に答える