swiftGrayImage

1
2
3
4
5
6
7
8
9
10
/// 彩色图片置灰,灰度图片
public func grayImage(sourceImage : UIImage) -> UIImage{
UIGraphicsBeginImageContext(self.size)
let colorSpace = CGColorSpaceCreateDeviceGray()
let context = CGContext(data: nil , width: Int(self.size.width), height: Int(self.size.height),bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.none.rawValue)
context?.draw(sourceImage.cgImage!, in: CGRect.init(x: 0, y: 0, width: sourceImage.size.width, height: sourceImage.size.height))
let cgImage = context!.makeImage()
let grayImage = UIImage.init(cgImage: cgImage!)
return grayImage
}