【问题标题】:Fill UIImage with UIColor用 UIColor 填充 UIImage
【发布时间】:2016-09-23 04:44:17
【问题描述】:

我有一个UIImage,我想用UIColor 填写

我试过这段代码,但应用程序在第 10 行崩溃。

代码如下:

extension  UIImage {
    func imageWithColor(_ color: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        let context = UIGraphicsGetCurrentContext()
        context?.translateBy(x: 0.0, y: size.height)
        context?.scaleBy(x: 1.0, y: -1.0)
        context?.setBlendMode(CGBlendMode.normal)
        let rect = CGRect(origin: CGPoint.zero, size: size)
        context?.clip(to: rect, mask: context as! CGImage)// crashes
        color.setFill()
        context?.fill(rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        return newImage!
    }
}

问题可能出在context?.clip(to: rect, mask: context as! CGImage) 上(我认为我不应该发送context 作为掩码,但我应该发送什么?CGImage()CGImage.self 都不起作用。

【问题讨论】:

  • 让我知道它是否有用let maskImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImagecontext?.clip(to: rect, mask: maskImage!)

标签: ios swift cocoa-touch uiimage swift3


【解决方案1】:

你必须这样做:

extension UIImage {
    func tinted(with color: UIColor) -> UIImage? {
        defer { UIGraphicsEndImageContext() }
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        withRenderingMode(.alwaysTemplate).draw(in: CGRect(origin: .zero, size: size))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

【讨论】:

    【解决方案2】:

    绘制完成后需要结束图像上下文:

    UIGraphicsEndImageContext();
    

    或者你可以为 UIImage 添加一个分类方法:

    - (UIImage *)imageByTintColor:(UIColor *)color
    {
        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
        CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
        [color set];
        UIRectFill(rect);
        [self drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeDestinationIn alpha:1];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    

    用作:

    image = [image imageByTintColor:color];
    

    【讨论】:

    • 所以context?.clip(to: rect, mask: context as! CGImage) 是正确的?我应该在哪里添加UIGraphicsEndImageContext() 行?
    • 之后 let newImage = UIGraphicsGetImageFromCurrentImageContext()
    • 仍然在第 10 行崩溃,我很确定我不应该在那里发送上下文,但也许我错了?
    • context?.clip(to: rect, mask: context as! CGImage)改成context?.clip(to: rect, mask: cgImage!),在swift3.0中就可以了。
    【解决方案3】:

    最简单的方法

    theImageView.image? = (theImageView.image?.imageWithRenderingMode(.AlwaysTemplate))! 
    theImageView.tintColor = UIColor.magentaColor()
    

    【讨论】:

    • 我希望它是UIImage,而不是UIImageView
    【解决方案4】:
    let image = UIImage(named: "whatever.png")?.imageWithRenderingMode(.alwaysTemplate)
    

    当您稍后将此图像设置为 UIButton 或 UIImageView - 只需更改该控件的色调颜色,图像将使用您指定的色调颜色绘制。

    【讨论】:

      猜你喜欢
      • 2018-05-02
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 2019-08-01
      相关资源
      最近更新 更多