【发布时间】: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()?.cgImage和context?.clip(to: rect, mask: maskImage!)
标签: ios swift cocoa-touch uiimage swift3