iOS:给图片置灰色
Posted 程序猿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS:给图片置灰色相关的知识,希望对你有一定的参考价值。
一、在ios开发中,给图片置灰色这个功能经常会用到,例如商品展示时,商品过期或者下线了,那么图片就需要这个功能。下面这个方法就可以到达目的。
/** UIImage:去色功能的实现(图片灰色显示) @param sourceImage 图片 */ - (UIImage *)grayImage:(UIImage *)sourceImage { int bitmapInfo = kCGImageAlphaNone; int width = sourceImage.size.width; int height = sourceImage.size.height; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); CGContextRef context = CGBitmapContextCreate (nil,width,height,8,0,colorSpace,bitmapInfo); CGColorSpaceRelease(colorSpace); if (context == NULL) { return nil; } CGContextDrawImage(context,CGRectMake(0, 0, width, height), sourceImage.CGImage); UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)]; CGContextRelease(context); return grayImage; }
实现效果:
之前:
之后:
以上是关于iOS:给图片置灰色的主要内容,如果未能解决你的问题,请参考以下文章