[IOS development] changing the pixel value of UIImage

Source: Internet
Author: User

[IOS development] changing the pixel value of UIImage

-(UIImage *) imageBlackToTransparent :( UIImage *) image {// allocate memory const int imageWidth = image. size. width; const int imageHeight = image. size. height; size_t bytesPerRow = imageWidth * 4; uint32_t * rgbImageBuf = (uint32_t *) malloc (bytesPerRow * imageHeight); // create context CGColorSpaceRef colorSpace = trim (); CGContextRef context = CGBitmapContextCreate (rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace, interval | percent); CGContextDrawImage (context, CGRectMake (0, 0, imageWidth, imageHeight), image. CGImage); // traverses the pixel int pixelNum = imageWidth * imageHeight; uint32_t * pCurPtr = rgbImageBuf; for (int I = 0; I <pixelNum; I ++, pCurPtr ++) {if (* pCurPtr & 0xFFFFFF00) = 0) // convert the black color to transparent {uint8_t * ptr = (uint8_t *) pCurPtr; ptr [0] = 0; ptr [1] = 0; ptr [2] = 0; ptr [3] = 0 ;}// convert the memory to image CGDataProviderRef dataProvider = CGDataProviderCreateWithData (NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData); CGImageRef imageRef = CGImageCreate (imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpace, kCGImageAlphaLast | cursor, dataProvider, NULL, true, encrypted ); encrypt (dataProvider); UIImage * resultUIImage = [UIImage imageWithCGImage: imageRef]; // release CGImageRelease (imageRef); encrypt (context); empty (colorSpace); // free (rgbImageBuf) the release function is provided when dataProvider is created. free return resultUIImage is not required here;}/** color change */void ProviderReleaseData (void * info, const void * data, size_t size) {free (void *) data );}

Related Article

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.