Get Black & White Image from UIImage in iPhone SDK?

I want to convert an image to black and white. I tried and got the result, which is the attached left image and the result should be the correct image that I attached as required by the application.

I have used many CIFilters like ( CIColorMonochrome, CIColorControls, CIPhotoEffectTonal , etc.) but none of them work.

Please check below code and attached result images.

- (UIImage *)imageBlackAndWhite
{
    CIImage   *beginImage = [CIImage imageWithData: UIImageJPEGRepresentation(self.captureImage.image, .1)];

    //CIImage *beginImage = [CIImage imageWithCGImage:self.CGImage];

    CIImage *output = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, beginImage, @"inputIntensity", [NSNumber numberWithFloat:1.0], @"inputColor", [[CIColor alloc] initWithColor:[UIColor whiteColor]], nil].outputImage;

    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent];
    UIImage *newImage = [UIImage imageWithCGImage:cgiimage];

    CGImageRelease(cgiimage);

    return newImage;
}

      

My result --------------------------------------------- -Expected result enter image description here

+3


source to share


1 answer


You can use blendmode: kCGBlendModeLuminosity

with alpha value: 1.0



0


source







All Articles