First, in the development of iOS, to the image gray this function is often used, such as product display, the product expired or offline, then the image needs this function. The following method can be used to achieve the purpose.
/** UIImage: The implementation of the color function (picture gray display) @param sourceimage pictures*/-(UIImage *) Grayimage: (UIImage *) sourceimage{intBitmapinfo =Kcgimagealphanone; intwidth =SourceImage.size.width; intHeight =SourceImage.size.height; Cgcolorspaceref ColorSpace=Cgcolorspacecreatedevicegray (); Cgcontextref Context= Cgbitmapcontextcreate (Nil,width,height,8,0, Colorspace,bitmapinfo); Cgcolorspacerelease (ColorSpace); if(Context = =NULL) { returnNil; } cgcontextdrawimage (Context,cgrectmake (0,0, width, height), sourceimage.cgimage); UIImage*grayimage =[UIImage imagewithcgimage:cgbitmapcontextcreateimage (context)]; Cgcontextrelease (context); returngrayimage;}
Implementation results:
Before:
After:
IOS: Gray for pictures