1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| +(UIImage*)systemImageToGray:(UIImage*)image { int width = image.size.width; int height = image.size.height; //第一步:开辟颜色空间 CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray(); //第二步:创建颜色空间的上下文 CGContextRef contextRef = CGBitmapContextCreate(nil, width, height, 8, 0,colorSpaceRef, kCGImageAlphaNone); if (contextRef == nil) { return nil; } //第三步:渲染图片 CGContextDrawImage(contextRef, CGRectMake(0, 0, width, height), image.CGImage); //第四步:创建图片 将绘制的颜色空间转成CGImage CGImageRef grayImageRef = CGBitmapContextCreateImage(contextRef); //第五步:将C/C++图片转成UIImage UIImage * newImage = [UIImage imageWithCGImage:grayImageRef]; //释放内存 CGColorSpaceRelease(colorSpaceRef); CGContextRelease(contextRef); CGImageRelease(grayImageRef); return newImage; }
|