CodeThe purpose is to generate a gray (black and white) uiimage based on a uiimage.
The following code is from a reply in stackoverflow. Unfortunately, the original link cannot be found.
-(Uiimage *) convertimagetogreyscale :( uiimage * ) Image { // Create Image rectangle with current image width/height Cgrect imagerect = cgrectmake ( 0 , 0 , Image. Size. Width, image. Size. Height ); // Grayscale Color Space Cgcolorspaceref colorspace = Cgcolorspacecreatedevicegray (); // Create bitmap content with current image size and grayscale colorspace Cgcontextref context = cgbitmapcontextcreate (nil, image. Size. Width, image. Size. height, 8 , 0 , Colorspace, kcgimagealphanone ); // Draw image into current context, with specified rectangle // Using previusly defined context (with grayscale colorspace) Cgcontextdrawimage (context, imagerect, [Image cgimage]); // Create bitmap image info from pixel data in current context Cgimageref imageref = Cgbitmapcontextcreateimage (context ); // Create a new uiimage object Uiimage * newimage = [Uiimage imagewithcgimage: imageref]; // Release colorspace, context and bitmap Information Cgcolorspacerelease (colorspace); cgcontextrelease (context); cfrelease (imageref ); // Return the new grayscale image Return Newimage ;}