Remove alpha channel from UIImage

In the options for the context, try the option

kCGImageAlphaNoneSkipLast

or

kCGImageAlphaNoneSkipFirst

Depending on if you are using RGBA or ARGB

ie,

CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
                                                       rect.size.width,
                                                       rect.size.height,
                                                       CGImageGetBitsPerComponent(imageRef),
                                                       CGImageGetBytesPerRow(imageRef),
                                                       CGImageGetColorSpace(imageRef),
                                                       kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Little
                                                       );

Same result but less code and things to worry about:

func removeAlpha(from inputImage: UIImage) -> UIImage {
        let format = UIGraphicsImageRendererFormat.init()
        format.opaque = true //Removes Alpha Channel
        format.scale = inputImage.scale //Keeps original image scale.
        let size = inputImage.size
        return UIGraphicsImageRenderer(size: size, format: format).image { _ in
            inputImage.draw(in: CGRect(origin: .zero, size: size))
        }
    }