How to covert alpha Image into 8 bit depth in iOS

I want to covert an image into 8 bit when I pick the image from the gallery or camera. Here is the code I have to tried. The below code is working without the alpha channel. If the image has an alpha, it's not working. And when I am picking an image from the camera there is some alpha value present so it throws an exception.

CGImageRef c = [[UIImage imageNamed:@"100_3077"] CGImage];
size_t bitsPerPixel = CGImageGetBitsPerPixel(c);
size_t bitsPerComponent = CGImageGetBitsPerComponent(c);
size_t width = CGImageGetWidth(c);
size_t height = CGImageGetHeight(c);

CGImageAlphaInfo a = CGImageGetAlphaInfo(c);

NSAssert(bitsPerPixel == 32 && bitsPerComponent == 8 && a == kCGImageAlphaNoneSkipLast, @"unsupported image type supplied");

CGContextRef targetImage = CGBitmapContextCreate(NULL, width, height, 8, 1 * CGImageGetWidth(c), CGColorSpaceCreateDeviceGray(), kCGImageAlphaNone);

UInt32 *sourceData = (UInt32*)[((__bridge_transfer NSData*) CGDataProviderCopyData(CGImageGetDataProvider(c))) bytes];
UInt32 *sourceDataPtr;

UInt8 *targetData = CGBitmapContextGetData(targetImage);

UInt8 r,g,b;
uint offset;
for (uint y = 0; y < height; y++)
{
    for (uint x = 0; x < width; x++)
    {
        offset = y * width + x;

        if (offset+2 < width * height)
        {
            sourceDataPtr = &sourceData[y * width + x];

            r = sourceDataPtr[0+0];
            g = sourceDataPtr[0+1];
            b = sourceDataPtr[0+2];

            targetData[y * width + x] = (r+g+b) / 3;
        }
    }
}

CGImageRef newImageRef = CGBitmapContextCreateImage(targetImage);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

I have tried this code. This code only converts an image into 8 bit when image CGImageAlphaInfo property is equal to kCGImageAlphaNoneSkipLast.