Jean Lebrument
Jean Lebrument

Reputation: 5169

Create an mask on an UIView to colorise all UIImageView subviews in black and white

I have an UIView which contains UIImagesViews with images loaded from a remote server.

The images loaded from the server are in color.

I would like create a mask on my UIView which put in black and white all its contents including the images.

But I don't want to have to put the mask on each picture when they are ready.

I tried to create a subclass of UIView :

@interface FriezeView : UIView

@property (nonatomic, strong) UIView *mask;
@property (nonatomic, strong) UIView *container;

@end

The init method of this class:

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        self.container = [[UIView alloc] initWithFrame:frame];
        self.mask = [[UIView alloc] initWithFrame:frame];

        [self addSubview:self.container];
        [self addSubview:self.mask];
    }

    return self;
}

Then I use the mask UIView to colorise the container UIView.

[_frieze.container addSubview:myImageView];

_frieze.mask.layer.backgroundColor = [[UIColor grayColor] CGColor];
_frieze.mask.backgroundColor = [UIColor grayColor];

But it doesn't work.

Any suggestions ?

Upvotes: 2

Views: 1406

Answers (1)

Duncan C
Duncan C

Reputation: 131436

No, you can't create a mask that will convert images to B&W.

What you CAN do is to write code that uses a Core Image Filter to convert the images to B&W.

You could use the "CIColorMonochrome" filter with an inputIntensity value 0 1, you could use "CIPhotoEffectMono", or you could use the "Color Controls" filter with an inputSaturation value of 0.

You COULD create a "MonochromeImage" subclass of UIImageView that would take an image as input and convert it to a monochrome equivalent for display.

I have a sample project called CIFilterTest (link) on github that lets you use all these filters and more. It should give you enough information to get started.

Upvotes: 3

Related Questions