Jak Mogę zmienić kolor obrazu

Otrzymuję obraz z serwera, wtedy na podstawie koloru wybranego przez użytkownika, kolor obrazu zostanie zmieniony.

Próbowałem:

_sketchImageView.image = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[_sketchImageView setTintColor:color];

Otrzymałem przeciwieństwo mojego celu (biały kolor poza UIImage jest zabarwiony wybranym kolorem).

Co się dzieje? Muszę zrobić to samo w tym pytaniu, podane rozwiązanie nie rozwiązuje mojej sprawy. Jak mogę zmienić kolor obrazu w iOS i WatchKit
Author: Community, 2015-02-10

10 answers

Spróbuj wygenerować nowy obraz dla siebie

UIImage *newImage = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale);
[yourTintColor set];
[newImage drawInRect:CGRectMake(0, 0, image.size.width, newImage.size.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

_sketchImageView.image = newImage;

I użyj go.

Powodzenia

======= Aktualizacja =======

To rozwiązanie spowoduje tylko zmianę koloru obrazu wszystkich pikseli.

Przykład: mamy obraz książki: http://pngimg.com/upload/book_PNG2113.png

Tutaj wpisz opis obrazka

I po uruchomieniu powyższego kodu (exp: TintColor jest czerwony). Mamy:

Tutaj wpisz opis obrazka

Więc: to, jak Twój wizerunek jest zależy od tego, jak zaprojektowałeś it

 47
Author: Tony,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-11 08:34:17

W języku Swift możesz użyć tego rozszerzenia: [na podstawie rozwiązania objective-c @ VietHung]


extension UIImage {
    func imageWithColor(color: UIColor) -> UIImage? {
        var image = imageWithRenderingMode(.AlwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        image.drawInRect(CGRect(x: 0, y: 0, width: size.width, height: size.height))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}
 17
Author: Mejdi Lassidi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-05 21:38:06

W swift 2.0 możesz użyć tego

let image = UIImage(named:"your image name")?.imageWithRenderingMode(.AlwaysTemplate)
let yourimageView.tintColor = UIColor.redColor()
yourimageView.image = image

W swift 3.0 możesz użyć tego

let image = UIImage(named:"your image name")?.withRenderingMode(.alwaysTemplate)
let yourimageView.tintColor = UIColor.red
yourimageView.image = image
 13
Author: Ruchin Somal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-15 07:50:12

Spróbuj czegoś takiego

UIImage *originalImage = _sketchImageView.image
UIImage *newImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)]; // your image size
imageView.tintColor = [UIColor redColor];  // or whatever color that has been selected
imageView.image = newImage;
_sketchImageView.image = imageView.image;
Mam nadzieję, że to pomoże.
 6
Author: Gismay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-10 12:18:33

W Swift 3.0 możesz użyć tego rozszerzenia: [na podstawie rozwiązania objective-c @ VietHung]

extension UIImage {
    func imageWithColor(_ color: UIColor) -> UIImage? {
        var image = imageWithRenderingMode(.alwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}
 3
Author: odemolliens,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-14 09:30:10

Możesz spróbować:

 _sketchImageView.image = [self imageNamed:@"imageName" withColor:[UIColor blackColor]];

 - (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
 {
     // load the image
     //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;
}
 2
Author: gabriel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-10 09:39:38

Dla Swift 3.0, zrobiłem niestandardową podklasę UIImageView o nazwie Tinteduiimageview. Teraz obraz używa dowolnego koloru tinty ustawionego w kreatorze interfejsów lub kodzie

class TintedUIImageView: UIImageView {

    override func awakeFromNib() {
        if let image = self.image {
            self.image = image.withRenderingMode(.alwaysTemplate)
        }
    }
}
 2
Author: tylerwalker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-05 19:24:39

Spróbuj ustawić kolor tinty na superwizorze widoku obrazu. Np. [self.view setTintColor:color];

 1
Author: Alf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-10 12:21:34

Oto jak aplikuję i używam tints w IOS 9 za pomocą Swift.

//apply a color to an image
//ref - http://stackoverflow.com/questions/28427935/how-can-i-change-image-tintcolor
//ref - https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming
func getTintedImage() -> UIImageView {

    var image     : UIImage;
    var imageView : UIImageView;

    image = UIImage(named: "someAsset")!;
    let size  : CGSize = image.size;
    let frame : CGRect = CGRectMake((UIScreen.mainScreen().bounds.width-86)/2, 600, size.width, size.height);

    let redCover : UIView = UIView(frame: frame);

    redCover.backgroundColor = UIColor.redColor();
    redCover.layer.opacity = 0.75;

    imageView       = UIImageView();
    imageView.image = image.imageWithRenderingMode(UIImageRenderingMode.Automatic);

    imageView.addSubview(redCover);

    return imageView;
}
 0
Author: J-Dizzle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-07 13:13:15

Jedną z rzeczy, które możesz zrobić, to po prostu dodaj swoje obrazy do folderu zasobów w XCode, a następnie zmień tryb renderowania na obraz szablonu, więc za każdym razem, gdy zmienisz kolor tinty UIImageView, automatycznie zmieni obraz.

Sprawdź ten link - > https://www.google.co.in/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwiM0YXO0ejTAhUIQ48KHfGpBpgQjRwIBw&url=https%3A%2F%2Fkrakendev.io%2Fblog%2F4-xcode-asset-catalog-secrets-you-need-to-know&psig=AFQjCNGnAzVn92pCqM8612o1R0J9q1y7cw&ust=1494619445516498

 0
Author: Rupesh Saxena,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-11 20:05:10