2

I don't want grayscale but rather for the darker colors to turn black and the lighter to turn white. How can I do this? I found something that looks promising here Getting a Black and White UIImage (Not Grayscale) but the line below in the code gives me an error that I can not fix.

CGContextRef contex = CreateARGBBitmapContext(image.size); 
Community
  • 1
  • 1
  • Search for the `CreateARGBBitmapContext` function in the docs. It's only found in a technical note from Apple. Simply copy the code from there. – rmaddy Oct 24 '15 at 04:06
  • Is citing oneself bad form? http://stackoverflow.com/questions/6672517/is-programmatically-inverting-the-colors-of-an-image-possible/6672628#6672628 should give you the information necessary to write a per-pixel filter. – Tommy Oct 24 '15 at 04:12
  • I get the error that implicit declaration is invalid in c99 when I run the code from the link I posted. – Vikram Mullick Oct 24 '15 at 04:21

3 Answers3

1

You should used GPUImage.

GPUImageAdaptiveThresholdFilter and GPUImageLuminanceThresholdFilter might be what you're looking for.

Example code:

UIImage *image = [UIImage imageNamed:@"yourimage.png"];
GPUImageLuminanceThresholdFilter *filter = [[GPUImageLuminanceThresholdFilter alloc] init];
UIImage *quickFilteredImage = [filter imageByFilteringImage:image];

Hope this helps!

Ty Lertwichaiworawit
  • 2,950
  • 2
  • 23
  • 42
0

You can convert your black and white image by using following code.

-(UIImage *)convertOriginalImageToBWImage:(UIImage *)originalImage
{
UIImage *newImage;

CGColorSpaceRef colorSapce = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, originalImage.size.width * originalImage.scale, originalImage.size.height * originalImage.scale, 8, originalImage.size.width * originalImage.scale, colorSapce, kCGImageAlphaNone);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, originalImage.size.width, originalImage.size.height), [originalImage CGImage]);

CGImageRef bwImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSapce);

UIImage *resultImage = [UIImage imageWithCGImage:bwImage];
CGImageRelease(bwImage);

UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, originalImage.scale);
[resultImage drawInRect:CGRectMake(0.0, 0.0, originalImage.size.width, originalImage.size.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


return newImage;
}

Or else you can use GPUImage framework .

It is a BSD-licensed iOS library that lets you apply GPU-accelerated filters and other effects to images, live camera video, and movies.

GPUImage framework

May be it will help you.

  • 1
    Doesn't this create a gray scale image instead of a black & white image? – rmaddy Oct 24 '15 at 04:17
  • Why are you creating a `newImage` from `resultImage` via UIGraphicsBeginImageContext? You already have an UIImage at that time. – benrudhart Dec 21 '16 at 06:49
-1

For those interested I created a Swift 3 version of @bHuMiCA solution:

extension UIImage {
    var bwImage: UIImage? {
        guard let cgImage = cgImage,
            let bwContext = bwContext else {
                return nil
        }

        let rect = CGRect(origin: .zero, size: size)
        bwContext.draw(cgImage, in: rect)
        let bwCgImage = bwContext.makeImage()

        return bwCgImage.flatMap { UIImage(cgImage: $0) }
    }

    private var bwContext: CGContext? {
        let bwContext = CGContext(data: nil,
                                  width: Int(size.width * scale),
                                  height: Int(size.height * scale),
                                  bitsPerComponent: 8,
                                  bytesPerRow: Int(size.width * scale),
                                  space: CGColorSpaceCreateDeviceGray(),
                                  bitmapInfo: CGImageAlphaInfo.none.rawValue)

        bwContext?.interpolationQuality = .high
        bwContext?.setShouldAntialias(false)

        return bwContext
    }
}
benrudhart
  • 1,406
  • 2
  • 13
  • 25