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:给图片置灰色的主要内容,如果未能解决你的问题,请参考以下文章

谷歌地图在Android的标签片段上显示为灰色

如何在 MS Word 文档中显示代码片段,因为它在 *** 中显示(滚动条和灰色背景)

想用css把网页做成灰色的,请问如何兼容IE10?

vscode中设置vue代码片段

如何在listview中设置文字内容的显示和字体颜色

谷歌地图在安卓设备上显示为灰色