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.
Related
I have researched a lot on how to add gradient in NavigationBar in iOS, but it doesn't show what I need. I want 2 color gradient with WHITE on TOP and CLEAR on BOTTOM. like this :
Tried using CAGradientLayer and also CRGradientNavigationBar,
but everything I have tried always shows White on TOP but when i set bottom color to CLEAR, it shows a weird black translucent color.
How to achieve similar NavigationBar like in the picture above?
Thanks!
Create gradient layer and add it as background of navigation bar.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.navigationController.navigationBar.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[self.navigationController.navigationBar setBackgroundImage:[self imageFromLayer:gradient] forBarMetrics:UIBarMetricsDefault];
For creating image from layer.
(UIImage *)imageFromLayer:(CALayer *)layer
{
UIGraphicsBeginImageContext([layer frame].size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
One more thing, there is one library available in github : CRGradientNavigationBar you can also use this library.
Got it! From this link. Actually clearColor has a black color channel with an alpha of 0, so instead use
[UIColor colorWithWhite:1 alpha:0]
and it will do the job! Thanks!
You can try creating a UIView on the top of your screen with the top constrain -70, and add CAGradientLayer inside it with it size.
then make your navigation bar transparent by adding UIImage()//empty image, to is as a background, this way it will show the UIView with grade CAGradientLayer in background
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)];
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];
I'm creating an iOS7 app and I have an UITextView inside a UITableView cell with a CAGradient applied to it. If I use different colors from clearColor works fine, but if I use clearColor as one of the colors for the gradient it turns out to be gray...
This is my code. theContent is my UITextView.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = theContent.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[theContent.layer insertSublayer:gradient atIndex:100];
And this is the final (wrong) result...
How can I fix this to have a clearColor to whiteColor gradient???
it may help on you if you try to use
[UIColor colorWithWhite:1.f alpha:0.f];
instead of the clearColor for the white tone.
I have the following code:
-(void)drawRect:(CGRect)rect
{
// gradient background
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = rect;
gradient.colors = [NSArray arrayWithObjects:(id) backgroundGradientTop.CGColor, (id) backgroundGradientBottom.CGColor, nil];
gradient.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.7], nil];
[self.layer insertSublayer:gradient atIndex:0];
// line on top
[[UIColor redColor] set];
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext, 5.0f);
CGContextMoveToPoint(currentContext, 0, 10.0f);
CGContextAddLineToPoint(currentContext, rect.size.width, 10.0f);
CGContextStrokePath(currentContext);
}
the line i'm trying to draw on top of the gradient is never shown. If i comment out the gradient layer it is there. Is there someway to draw both a gradient background and a line (or a few lines) on top? Maybe i shouldn't be mixing calayer and CG?
The line i'm trying to draw on top of the gradient is never shown. If i comment out the gradient layer it is there.
That's because sublayers appear on top of their parent layers. Your gradient is apparently opaque, and the same size as your view, so it covers up the view.
You can't mix CA and CG drawing this way. It would work better if you drew the gradient using CGContextDrawLinearGradient.