Create gradient on UIImage for text - ios

I am trying to replicate the gradient found on the bottom of these album artwork image views. It makes it very easy to read the text no matter what the background is.
How would I recreate this?

The gradient effect is known as a floor fade.
The gradient starts with 0.0 alpha black at the middle of the image, to about 0.2 alpha black at the bottom of the image.
You could add a CAGradientLayer to your image, along the lines of:
CAGradientLayer *bottomFade = [CAGradientLayer layer];
bottomFade.frame = CGRectMake(0.0, CGRectGetHeight(self.imageView.bounds), CGRectGetWidth(self.imageView.bounds), -(CGRectGetHeight(self.imageView.bounds) / 2.0));
bottomFade.startPoint = CGPointMake(0.5, 1.0);
bottomFade.endPoint = CGPointMake(0.5, 0.0);
bottomFade.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.0f] CGColor], (id)[[UIColor colorWithWhite:0.0 alpha:0.2f] CGColor], nil];
[image.layer addSublayer:bottomFade];

Related

Stuck in UIImageView masking multiple images

I am working on UIImageView masking. Where i want image background like below.
You can see the black effect at bottom on text "Himalaya Gel".
I want to add such effect every time i add image.
So if anybody know's the solution pls help.
Thanks in advance.
You can use a gradient on the UIImageView, any time you change the UIImage, the gradient will still stay.
- (void)addGradientToView:(UIView *)view
{
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, view.frame.size.height * 0.8, view.frame.size.width, view.frame.size.height * 0.2);
gradient.colors = #[(id)[[UIColor clearColor] CGColor],
(id)[[UIColor blackColor] CGColor]];
[view.layer insertSublayer:gradient atIndex:0];
}
The gradient will only start on the bottom of the view (view.hieght * 0.8) with a clear color, and continue till the end will black gradient.

How I can make a gradient with three opposite points?

I want to make a color picker just like in the picture, but I could not do the triangle, which has three points to show the gradient, White, Black and the selected color.
The black and white are always the same, but the color selected should vary according to the user selects in the circle, but do not know how to make a gradient opposite to those three points, and displays the image.
(Only objective c, please)
Hope, someone could help me. Thanks.
HSL Picker
Here is what you can try to create gradient with three colors:
Get the reference from your triangle edges that have the reference to the color they point to.
BOOL isHorizontal=YES;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[self.startColor CGColor],(id)[self.midColor CGColor], (id)[self.endColor CGColor], nil];
gradient.endPoint = (self.isHorizontal) ? CGPointMake(1, 0) : CGPointMake(0, 1);
[self.layer insertSublayer:gradient atIndex:0];
If you want to change the gradient direction, play around with:
[gradient setStartPoint:CGPointMake(0.0, 0.5)];
[gradient setEndPoint:CGPointMake(1.0, 0.5)];

How to create a gradient

I have this code to create a gradient background
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
UIColor *startColour = [UIColor colorWithHexString:#"#bcdef6"];
UIColor *endColour = [UIColor colorWithHexString:#"#fad7e6"];
UIColor *middleColor = [UIColor colorWithHexString:#"#ffffff"];
gradient.colors = [NSArray arrayWithObjects:(id)[startColour CGColor],(id)middleColor, (id)[endColour CGColor], nil];
gradient.endPoint = CGPointMake(1, 0);
gradient.startPoint = CGPointMake(0, 1);
[self.layer insertSublayer:gradient atIndex:0];
how do i get rid of the greyish effect in the middle ?
this is the desired result:
this is the actual result:
I can see a bug with your colors array, you have not taken the CGColor of the middle color (white). By changing the array to this, I can get your desired result.
gradient.colors = #[(id)startColour.CGColor, (id)middleColor.CGColor, (id)endColour.CGColor];
By playing around with the gradient locations, you can also see more of the blue and pink colors should that create a better effect.
gradient.locations = #[#0.2f, #0.5f, #0.8f];

Apply a blur gradient

I need to mask an image with a blur gradient.
In more detail; I want the picture to start on the left with no blur at all and on its right to be blurred. The blurring will start to occur somewhere half way. I already managed to blur the image in full but as a separate image, but how do I apply a semi-transparent gradient of that blurring?
UIImageView *bluredImgView = [[UIImageView alloc] initWithImage:img];
bluredImgView.frame = frame;
CAGradientLayer *lay = [CAGradientLayer layer];
lay.frame = bluredImgView.bounds;
lay.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite: 0.0 alpha: 0.0] CGColor], (id)[[UIColor colorWithWhite: 1.0 alpha: 1.0] CGColor], nil];
lay.startPoint = CGPointMake(0.0f, 0.0f);
lay.endPoint = CGPointMake(1.0f, 0.0f);
bluredImgView.layer.mask = lay;
[_profileImageView addSubview:bluredImgView];
This question is rather old but still.. None of the previous answers combined the blur effect to a gradient.
This adds the effect from bottom to top (looks cool for title images):
titleImage.image = [UIImage imageNamed: titleImage];
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *view = [[UIVisualEffectView alloc] initWithEffect:effect];
view.frame = titleImage.frame;
UIImageView *bluredImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_location.titleImage]];
bluredImgView.frame = _titleImage.frame;
CAGradientLayer *lay = [CAGradientLayer layer];
lay.frame = bluredImgView.bounds;
lay.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite: 0.0 alpha: 0.0] CGColor], (id)[[UIColor colorWithWhite: 0.0 alpha: 1.0] CGColor], nil];
lay.startPoint = CGPointMake(0.0f, 0.0f);
lay.endPoint = CGPointMake(0.0f, 1.0f);
view.layer.mask = lay;
[titleImage addSubview:view];
Add the blurred image as a separate layer on top of your image view's layer. Then create a CAGradientLayer and add it as a mask layer on your blurred image layer.
Use code below:
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)[[UIColor colorWithWhite:0 alpha:0] CGColor], (__bridge id)[[UIColor colorWithWhite:0 alpha:1] CGColor], nil];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
[gradientLayer setFrame:[bluredImgView bounds]];
[gradientLayer setColors:colors];
[gradientLayer setStartPoint:CGPointMake(0.0f, 0.0f)];
[gradientLayer setEndPoint:CGPointMake(1.0f, 0.0f)];
bluredImgView.layer.mask = gradientLayer;

CAGradientLayer showing as solid color

I'm trying to set a gradient background on a view, but my code below makes the UIView appear solid black. If i change the whiteColor to greenColor, the gradient is displayed properly. Changing the backgroundColor of the layer to greenColor makes the view appear solid green.
My guess is that i'm dealing with some kind of transparency-issue, but i can't figure to work around it.
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = [NSArray arrayWithObjects:
(id)[[UIColor whiteColor] CGColor],
(id)[[UIColor blackColor] CGColor], nil];
gradientLayer.frame = CGRectMake(0, 0, 1, someView.frame.size.height);
UIGraphicsBeginImageContext(CGSizeMake(1, someView.frame.size.height));
[gradientLayer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
someView.backgroundColor = [UIColor colorWithPatternImage: image];
Your white and black colors are in grayscale colorspace. Try making them in RGB colorspace:
gradientLayer.colors = #[
(id)[UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor,
(id)[UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor
];
This should fix your issue, though I'm not sure of the Quartz mechanics behinds this.

Resources