How can I change image tintColor

后端 未结 13 763
时光说笑
时光说笑 2020-12-13 00:04

I\'m receiving image from a server, then based on a color chosen by the user, the image color will be changed.

I tried the following :

_sketchImageVi         


        
相关标签:
13条回答
  • 2020-12-13 00:46

    For Swift 3.0, I made a custom subclass of UIImageView called TintedUIImageView. Now the image uses whatever tint color is set in interface builder or code

    class TintedUIImageView: UIImageView {
    
        override func awakeFromNib() {
            if let image = self.image {
                self.image = image.withRenderingMode(.alwaysTemplate)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-13 00:46

    Here's how I apply and use tints in IOS 9 with 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 讨论(0)
  • 2020-12-13 00:47

    Try to generate new image for yourself

    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;
    

    And use it.

    Good luck

    ======= UPDATE =======

    This solution will only change color of all pixel's image.

    Example: we have a book image: http://pngimg.com/upload/book_PNG2113.png

    enter image description here

    And after running above code (exp: TintColor is RED). We have:

    enter image description here

    SO: how your image is depends on how you designed it

    0 讨论(0)
  • 2020-12-13 00:48

    In swift 2.0 you can use this

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

    In swift 3.0 you can use this

    let image = UIImage(named:"your image name")?.withRenderingMode(.alwaysTemplate)
    let yourimageView.tintColor = UIColor.red
    yourimageView.image = image
    
    0 讨论(0)
  • 2020-12-13 00:53

    In Swift you can use this extension: [Based on @VietHung's objective-c solution]

    Swift 5:

    extension UIImage {
          func imageWithColor(color: UIColor) -> UIImage? {
            var image = withRenderingMode(.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
        }
    }
    

    Previous Swift version:

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-13 00:57

    Try something like this

    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;
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题