有没有办法以灰度绘制给定的 UIView (及其子视图)?
我正在尝试采用视图层次结构并使其看起来已禁用,而无需覆盖图像或切换用于绘制层次结构中每个 UIView 的图像。
我正在为iOS应用程序构建扫描仪组件到目前为止,我已经裁剪了结果图像,并且正确的视角.现在我需要将彩色图像变成黑白"扫描"文档.
我尝试使用 - "CIPhotoEffectNoir",但它更灰度,然后完全黑白.我希望获得100%黑色和100%白色的完整对比图像.
我怎样才能做到这一点?谢谢
我正在尝试将 UIImage 转换为灰度。它所产生的只是一个完全黑色的图像。
func convertToGrayScale2(image: UIImage) -> UIImage {
let imageRect:CGRect = CGRect(x:0, y:0, width:image.size.width, height: image.size.height)
let colorSpace = CGColorSpaceCreateDeviceGray()
let width = image.size.width
let height = image.size.height
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)
let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
let imageRef = context!.makeImage()
context?.draw(imageRef!, in: imageRect)
let newImage = UIImage(cgImage: imageRef!)
return newImage
}
Run Code Online (Sandbox Code Playgroud)
代码有什么问题吗?我需要现有代码的帮助。它之前正在工作。突然它不工作了。