Generating gray (black and white) images in IOS

Source: Internet
Author: User

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 ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.