【发布时间】:2018-07-09 19:12:31
【问题描述】:
我有一个 UIImage 扩展程序,它可以更改我在某处提取的图像的颜色。问题是它在为图像着色后会降低分辨率。我已经看到了基于此的其他答案,但我不确定如何使其适应在这种情况下渲染视网膜图像:
extension UIImage {
func maskWithColor(color: UIColor) -> UIImage? {
let maskImage = cgImage!
let width = size.width
let height = size.height
let bounds = CGRect(x: 0, y: 0, width: width, height: height)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!
context.clip(to: bounds, mask: maskImage)
context.setFillColor(color.cgColor)
context.fill(bounds)
if let cgImage = context.makeImage() {
let coloredImage = UIImage(cgImage: cgImage)
return coloredImage
} else {
return nil
}
}
}
我见过有人使用 UIGraphicsBeginImageContextWithOptions 并将其比例设置到主屏幕,但如果我使用 CGContext 函数,我认为它不起作用。
【问题讨论】:
-
拒绝投票和关闭请求在没有解释的情况下根本没有帮助。
标签: swift cgcontext cgimage retina