我有这个扩展(找到obj-c并将其转换为Swift3)以获得相同UIImage但灰度:
public func getGrayScale() -> UIImage
{
let imgRect = CGRect(x: 0, y: 0, width: width, height: height)
let colorSpace = CGColorSpaceCreateDeviceGray()
let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue).rawValue)
context?.draw(self.cgImage!, in: imgRect)
let imageRef = context!.makeImage()
let newImg = UIImage(cgImage: imageRef!)
return newImg
}
Run Code Online (Sandbox Code Playgroud)
我可以看到灰色图像,但它的质量非常糟糕......我唯一能看到的与质量相关的是bitsPerComponent: 8在上下文构造函数中.然而,看看Apple的文档,这是我得到的:
它表明iOS只支持8bpc ......那么为什么我不能提高质量呢?