相关疑难解决方法(0)

将UIImage转换为灰度,保持图像质量

我有这个扩展(找到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 ......那么为什么我不能提高质量呢?

uiimage grayscale swift

19
推荐指数
6
解决办法
2万
查看次数

标签 统计

grayscale ×1

swift ×1

uiimage ×1