web-dev-qa-db-ja.com

iPhone-画像にどのように色を付けますか?

画像に色を付ける方法を知りたいです(たとえば、白の.pngを赤にします)。私はさまざまな提案を見てきましたが、これが実際に可能であるという確認はありません。私はこれを試しました:

-(UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor {
    UIGraphicsBeginImageContext(baseImage.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);

    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    CGContextSaveGState(ctx);
    CGContextClipToMask(ctx, area, baseImage.CGImage);
    [theColor set];
    CGContextFillRect(ctx, area);
    CGContextRestoreGState(ctx);
    CGContextSetBlendMode(ctx, kCGBlendModeNormal);
    CGContextDrawImage(ctx, area, baseImage.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

myImageView.image = [self colorizeImage:[UIImage imageNamed:@"whiteImage.png"] color:[UIColor redColor]];

しかし、それは機能しません-画像はまだ画面上で白いです。

35
sol

これをUIImageカテゴリにします。また、iOS4のスケールファクターも考慮に入れています。

- (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)])  // The scale property is new with iOS4.
            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 *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

[〜#〜] update [〜#〜]-Swiftバージョン:

func imageWithColor(color: UIColor) -> UIImage {
    let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
    UIGraphicsBeginImageContextWithOptions(self.size, false, 0.0);
    drawInRect(rect)
    let context = UIGraphicsGetCurrentContext()
    CGContextSetBlendMode(context, .SourceIn)
    CGContextSetFillColorWithColor(context, color.CGColor)
    CGContextFillRect(context, rect);
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}
64
Dave Batton

ブレンドモードをmultiplyに変更すると、コードは機能します。

CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
12
willc2

IOS 7以降、画像を単色で色付けすることができます。

Objective C

UIImage *image = [UIImage imageNamed:@"myImage.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
imageView.tintColor = [UIColor redColor];

Swift

let image = UIImage(named:"myImage.png")?.withRenderingMode(.alwaysTemplate)
let imageView = UIImageView(image:image)
imageView.tintColor = .red

これにより、画像全体が1つの色で着色され、アルファチャネルが保持されます。

10
Magoo

IOS7から画像に色を付ける最良の方法は、画像にプロパティIImageRenderingModeAlwaysTemplateを指定することだと思います。

myImageView.image = [[UIImage imageNamed:@"myImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

次に、imageViewのtintColorで色付けします

myImageView.tintColor = [UIColor purpleColor]; //Maybe purple is not the best choice ;)

私はそれがより簡単で(カテゴリーは必要ありません)そしてもちろん最適化されていると思います!

注:xcassetsを使用している場合は、次のスクリーンショットのように、XCodeでレンダリングプロパティをIImageRenderingModeAlwaysTemplateに設定できます。

UIImageRenderingModeAlwaysTemplate in XCode

2
andreacipriani

@オタクの子供、これがあなたの男に役立つかどうか私に知らせてください...

_// 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);
_

//「context」をUIGraphicsGetCurrentContext()に置き換えるか、uに現在のコンテキストのインスタンスがある場合。

お役に立てば幸いです。

1
Ram

さて、これはどうですか?

アセット作成フェーズ(アプリケーションの実行中は動的ではありません)で、画像の配色を反転します。白->透明になった部分。透明になった部分->ページの背景が何であれ。

各画像の下に、同じサイズの空白の白いビューを配置します。画像を赤にしたい場合は、その空白の白いビューの色を赤に変更します。

それは実行可能ですか?

1
Amagrammer

短く、甘く、より良い:)

- (UIImage *)colorizeImage:(UIImage *)image withColor:(UIColor *)color

16進カラーコードを使用するようにUIColorFromRGBを定義してから、colorizeImage:withColorを呼び出すだけです。

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage *imgToColor = [UIImage imageNamed:@"myImage.png"];
    imgToColor = [self colorizeImage:imgToColor withColor:UIColorFromRGB(0xff00ff)];
    // use normal UIColor or UIColorFromRGB() for hex
}

 - (UIImage *)colorizeImage:(UIImage *)image withColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
    [image drawInRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(context, kCGBlendModeSourceIn);

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
1
emotality

UIImageViewのbackgroundColorを[UIColorredColor]に設定すると、元のPNGのtransparentであった部分が赤になります。または、imageviewのサブビューとして背景色が赤の半透明のUIViewを追加してみることもできます。これにより、画像を表示しているビューに赤いもやが追加されます。

0
drvdijk

最初に色を描画し、次にその上に画像を描画します。画像が部分的に透明でない限り、背景色が完全に上書きされます。最初に元の画像を描画してから、kCGBlendModeHueを使用してその上に赤を描画することをお勧めします。

なぜあなたがあなたの文脈をひっくり返して翻訳しているのか私にはわかりません。あなたの写真は逆さまですか?

0
Rob Napier