iPhone - Wie färbt man ein Bild ein?

Ich würde gerne wissen, wie man ein Bild einfärbt (zum Beispiel ein weißes .png-Rot). Ich habe verschiedene Vorschläge gesehen, aber nie eine Bestätigung, dass dies tatsächlich möglich ist. Ich habe das versucht:

-(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]];

Aber es funktioniert nicht - das Bild ist immer noch weiß auf dem Bildschirm.

Antworten auf die Frage(9)

Ihre Antwort auf die Frage