I need to get a pure black and white UIImage from another UIImage (not grayscale). Can anybody help me?
Thank you for reading.
Edition:
Here is a suggested solution. Thanks to everyone. Almost I know that this is not the best way to do this, it works great.
// Gets an pure black and white image from an original image. - (UIImage *)pureBlackAndWhiteImage:(UIImage *)image { unsigned char *dataBitmap = [self bitmapFromImage:image]; for (int i = 0; i < image.size.width * image.size.height * 4; i += 4) { if ((dataBitmap[i + 1] + dataBitmap[i + 2] + dataBitmap[i + 3]) < (255 * 3 / 2)) { dataBitmap[i + 1] = 0; dataBitmap[i + 2] = 0; dataBitmap[i + 3] = 0; } else { dataBitmap[i + 1] = 255; dataBitmap[i + 2] = 255; dataBitmap[i + 3] = 255; } } image = [self imageWithBits:dataBitmap withSize:image.size]; return image; }
EDITED 1:
In response to the comments, Here are the bitmapFromImage and imageWithBits .
// Retrieves the bits from the context once the image has been drawn. - (unsigned char *)bitmapFromImage:(UIImage *)image { // Creates a bitmap from the given image. CGContextRef contex = CreateARGBBitmapContext(image.size); if (contex == NULL) { return NULL; } CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height); CGContextDrawImage(contex, rect, image.CGImage); unsigned char *data = CGBitmapContextGetData(contex); CGContextRelease(contex); return data; } // Fills an image with bits. - (UIImage *)imageWithBits:(unsigned char *)bits withSize:(CGSize)size { // Creates a color space CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); if (colorSpace == NULL) { fprintf(stderr, "Error allocating color space\n"); free(bits); return nil; } CGContextRef context = CGBitmapContextCreate (bits, size.width, size.height, 8, size.width * 4, colorSpace, kCGImageAlphaPremultipliedFirst); if (context == NULL) { fprintf (stderr, "Error. Context not created\n"); free (bits); CGColorSpaceRelease(colorSpace ); return nil; } CGColorSpaceRelease(colorSpace ); CGImageRef ref = CGBitmapContextCreateImage(context); free(CGBitmapContextGetData(context)); CGContextRelease(context); UIImage *img = [UIImage imageWithCGImage:ref]; CFRelease(ref); return img; }
source share