Search code examples
iosios6

Distorted image appears on top of view, only on iOS 6


I am developing an iOS application that must support iOS 7 and 6. The application works well under iOS 7 but I encounter a really strange bug under iOS 6.

At the top of almost every view there is a distorted image that appears. I, of course, never added these images in the view hierarchy at these positions. The images are actually icons used in my storyboard.

Also you may notice that the UI looks like iOS 7 even if the following screenshots are from the iOS 6 simulator. This is because I am using iOS7Kit. I tried disabling it, ant it is not the cause of the problem.

The problem also appears on physical iOS 6 devices, so this is not a bug of the simulator.

I don't know what to look for to solve this bug... Any idea?

In this case the distorted image comes from the menu:

Distorted image at the top of the gallery view.

Screenshot of my storyboard menu view (uses a container view as you can see):

Storyboard menu view!

On the settings view the distorted image comes from the settings view itself:

Distorted image at the top of the settings view

Screenshot of my storyboard settings view (also uses a container view):

Storyboard settings view


Solution

  • Finally found out the problem when I noticed that the distorted images were only the images that I tint programmatically to get the "selected" color.

    I was using the following function (from a SO answer if I remember well):

    - (UIImage *)colorImageWithColor:(UIColor *)color
    {
        // Make a rectangle the size of the image
        CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
        // Create a new bitmap context based on the current image's size and scale, that has opacity
        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
        // Get a reference to the current context (which we just created)
        CGContextRef context = UIGraphicsGetCurrentContext();
        // Draw the image into the context we created
        [self drawInRect:rect];
        // Set the fill color of the context
        CGContextSetFillColorWithColor(context, [color CGColor]);
        // This sets the blend mode, which is not super helpful. Basically it uses your fill color with the alpha of the image and vice versa.
        CGContextSetBlendMode(context, kCGBlendModeSourceAtop);
        // Now apply the color and blend mode onto the context.
        CGContextFillRect(context, rect);
        // Grab the result of all this drawing from the context.
        UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
        // Destroy the bitmap context
        UIGraphicsEndImageContext();
        return result;
    }
    

    The UIGraphicsEndImageContext(); call was missing. Adding it before the return of the function fixes the problem.