Search code examples
iphoneobjective-ccore-graphicscontrast

How to programmatically change contrast of PNG image of without background in iPhone objective-c?


I want to programmatically change the contrast of a PNG image which has no background or I must say transparent background. The below image shows the example.enter image description here

I am able to change the contrast of with background image. But not able to change it in without background. I have tried this and this But both works in with background Image. Please help me to achieve this or suggest me any solution or tutorial link.

Thanks in advance


Solution

  • CGImageRef inImage = self.CGImage;
    CFDataRef m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));  
    UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);  
    
    int length = CFDataGetLength(m_DataRef);
    
    for (int i=0; i<length; i+=4)
    {
        filter(m_PixelBuf,i,context);
    }  
    CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf,  
                                             CGImageGetWidth(inImage),  
                                             CGImageGetHeight(inImage),  
                                             CGImageGetBitsPerComponent(inImage),
                                             CGImageGetBytesPerRow(inImage),  
                                             CGImageGetColorSpace(inImage),
                                             CGImageGetBitmapInfo(inImage) );     
    
    CGImageRef imageRef = CGBitmapContextCreateImage(ctx);  
    CGContextRelease(ctx);
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CFRelease(m_DataRef);
    return finalImage;