bmaslakov/cocoa-close-pixelate

Pixelated image is blue since iOS 11.4

Opened this issue · 1 comments

Hello,

I seem to have an issue when using your lib since iOS 11.4, when I pixelate an image, the image is blue.

Here's my code:

extension UIImage {
    
    func pixelate(resolution: CGFloat) -> UIImage? {
        guard let cgImage = self.cgImage,
            let pixelatedImage = Pixelate.create(pixels: cgImage, layers: PixelateLayer(.square, resolution: resolution))
            else { return nil }
        
        return UIImage(cgImage: pixelatedImage, scale: self.scale, orientation: self.imageOrientation)
    }
    
}

When I use this code to pixelate an image from an UIImage instance, like this for example:

let image = UIImage(named: "imageName")
let pixelatedImage = image.pixelate(resolution: 10)

imageView.image = pixelatedImage

The image showed in the image view is completely blue. It's correctly pixelated, but blue. An it seems to have appeared with iOS 11.4, it was working fine before.

Any idea why?

It seems like there is an issue when interpreting a CGImage with alphaInfo == noneSkipFirst in Pixelate.swift file on line 169.
According to Apple, there is more treatment to do:

There is no alpha channel. If the total size of the pixel is greater than the space required for the number of color components in the color space, the most significant bits are ignored.

A workaround for this would be: reload your image with UIImageJPEGRepresentation(image, 1.0) to get a normal alphaInfo such as noneSkipLast which works well.