Basic issue:
I believe the trick is with masking, but I am not able to get a good hold of how this is set.
Basically I have a bright image (set to a uiimageview object), and I have a label at very bottom (which is added on top of the image view) needs a well readable white text on it. Right now, the white text is hard to read (because of the bright background).
What I am doing:
I am setting a mask for the image view with something like
http://cl.ly/image/0i0N1p271d42
maskContainer = [CALayer layer];
UIImage *maskImg = [UIImage imageNamed:#"mask_profile"];
[maskContainer setContents:(id)[maskImg CGImage]];
CGRect frma = maskContainer.frame;
frma.size.width = self.frame.size.width;
frma.size.height = self.frame.size.height;
maskContainer.frame = frma;
[self.imageView.layer setMask:maskContainer];
Its messed up. The overall image starts fading on top.
Can anyone share their insight on the right way to mask?
You could set a drop shadow on your text to make is stand out even over a white background:
myLabel.layer.shadowOpacity = 0.8f;
myLabel.layer.shadowOffset = CGSizeMake(0, 0);
Easiest option is to adjust the alpha on the UILabel to the desired darkness in order to make the text stand out. If you do not want to hide the image and the image itself serves as a dark background, then set the alpha on the label to 0.
The best way to do this is to place the label in a uiview then create a gradient to apply as the background to the uiview. You can create the gradient as either an image with transparency or you can draw it in code. This will create a darkening effect on you bright image just behind the label so the text will pop.
Related
I want to create a layer to act as a mask for my UIImageView which was created in interface builder. However, no matter what I do the layer remains white. The code is pretty straightforward any ideas what is causing this behavior ?
UIImage* image = [UIImage imageNamed:#"face"];
self.imageView.image = image;
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = self.imageView.bounds;
maskLayer.fillColor = [[UIColor blackColor] CGColor];
maskLayer.path = CGPathCreateWithRect(self.imageView.bounds, NULL);
self.imageView.layer.mask = maskLayer;
self.maskLayer = maskLayer;
I edited the code and added the path but it still does not work.
You seem to have a misconception as to what a mask is. It is a set of instructions, in effect, for injecting transparency into a view, thus "punching a hole" through the view to a greater or lesser extent (depending on the degree of transparency). It has no color. You are seeing white because that is the color of what is behind your image view — you have punched a hole through the entire image view and made it invisible.
From the docs:
The layer’s alpha channel determines how much of the layer’s content
and background shows through. Fully or partially opaque pixels allow
the underlying content to show through but fully transparent pixels
block that content.
Black fill (interior of the closed path) = opaque pixels
Area outside of the closed path = transparent pixels
Image will appear on the interior region of your path and not appear outside of that path. The actual colour is irrelevant: when you set a layer as a mask, all we are interested in now is opacity.
The path is ostensively the same size as the imageView, so you won't see any difference as the mask matches the imageView bounds. Additionally , if you use this code before the geometry is fully set, such as in viewDidLoad, you may not get the results you expect.
As matt suggests - you need to think what result you are after.
If you want a "black translucent color" - or similar - consider adding another translucent-colored layer to the mix. But this is not a mask.
In my table, I'm currently setting the background of the cells as an image like so:
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Placeholder.png"]];
However, the white text that overlays this image in the cell is hard to read, so I'd like to add a dimming effect to it.
As far as I know, the closest thing that's available is UIBlurEffectStyleLight, but this blurs rather than dims, and the blur effect is too strong, making the image barely visible. Is there a way to dim it instead?
You might want to add a UIView between the image and the text, then give it a black background colour and set its alpha to like 0.3 or an appropriate amount
Reduce the alpha of the imageView
cell.backgroundView.alpha = 0.5; //Alpha runs from 0.0 to 1.0
I want to show an image of glass getting filled with water in iPhone using iOS 8. I can get a white background image of glass (that shows empty glass) and water color could be light grey, for example.
Is there an option to fill background color of glass (in intervals)
so that it looks as if the glass is really getting filled with water?
Can I draw complete image (glass already filled with water) part by
part from bottom to top to show similar effect?
Any options to display/animate such an image?
With UIImageView you can do like that, create an images for each frame:
images = #[[UIImage imageNamed:#"img1.png"],
[UIImage imageNamed:#"img2.png"],
[UIImage imageNamed:#"img3.png"]];
[imageView setAnimationImages:testArray] ;
imageView.animationDuration = 0.5;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
Another way is to have UIView's/UIImageView's composition, e.g. UIImageView in front, use it as mask, another one in a back, and just use simple UIView animation.
But anyway, such a animation can be done in many ways, depends on your needs and how nice this animation should be.
I would like to how can I apply this effect in iPhone SDK?
So, I have an image and a label on top of it.
I want to have the effect in which the bottom portion kind of blends in with the image.
So that there is no clear demarcation from where the image ends at the bottom portion of the view.
Please let me know.
Easy way to achieve this to CAGradientLayer
UIView *yourGradientView; // with that label "ENTREES", Add this view as a subview of the background view.
CAGradientLayer *gradientLayer=[CAGradientLayer layer];
[gradientLayer setFrame:[yourGradientView bounds]];
[gradientLayer setColors:#[(id)[UIColor clearColor].CGColor, (id)[[UIColor whiteColor] colorWithAlphaComponent:0.7f].CGColor]];
[gradientLayer setLocations:#[[NSNumber numberWithFloat:0.50f], [NSNumber numberWithFloat:1.0f]]];
[[yourGradientView layer] insertSublayer:gradientLayer atIndex:0];
Easiest solution: Add UIImageView with gradient PNG image as a subview between the image and label if you have the constant color.
If you need variable color of the gradient, you can either add a subview with the gradient drawn using CoreGraphics or CALayer.
If you need the image to blend with any background, you can mask the background image layer with CALayer gradient layer.
Unless you provide any more details to your question with regards to functionality and some code, the first look instance seems to have the following solution:
Step 1
Set a UIImage as a background image. In your case it is the one shown in the question.
Step 2 add a UILabel as a subview of UIImage and set the background of UILabel to be transparent. Position the label as per your needs which in your case seems to be the bootom left.
Hope his helps !!!
I've seen similar questions, but haven't found workable answers.
I want to mask a UIView using a grey image (need to convert to alpha scale for masking). The UIView has background. It should be easy to mask an image, but I want to mask any UIView.
Any clues will be appreciated.
I've been working on this problem for a couple of hours and have a solution that I think will do what you want. First, create your masking image using whatever means you see fit. Note that we only need the alpha values here, all other colours will be ignored, so make certain that the method you use supports alpha values. In this example I'm loading from a .png file, but don't try it with .jpg files as they don't have alpha values.
Next, create a new layer, assign your mask to its contents and set this new layer to your UIView's own layer, like so: you should find that this masks the UIView and all its attached subviews:
UIImage *_maskingImage = [UIImage imageNamed:#"mask"];
CALayer *_maskingLayer = [CALayer layer];
_maskingLayer.frame = theView.bounds;
[_maskingLayer setContents:(id)[_maskingImage CGImage]];
[theView.layer setMask:_maskingLayer];
With this done, you can set the UIView's background colour to whatever you like and the mask will be used to create a coloured filter.
EDIT: As of iOS8 you can now mask a view simply by assigning another view to its maskView property. The general rules stay the same in that the maskView's alpha layer is used to determine the opacity of the view it is applied to.
For apps targeting iOS 8.0+ this worked well (in this case, using a gradient as the mask) It avoids any need to resize or position the mask.
// Add gradient mask to view
func addGradientMask(targetView: UIView)
{
let gradientMask = CAGradientLayer()
gradientMask.frame = targetView.bounds
gradientMask.colors = [UIColor.blackColor().CGColor, UIColor.clearColor().CGColor]
gradientMask.locations = [0.8, 1.0]
let maskView: UIView = UIView()
maskView.layer.addSublayer(gradientMask)
targetView.maskView = maskView
}
In my case, I want to remove the mask once the user starts scrolling. This is done with:
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
exerDetailsTableView.maskView = nil
}
where the view is defined as an #IBOutlet:
#IBOutlet weak var exerDetailsTableView: UITableView!
Result:
I don't know the exact code off the top of my head but the basic idea is to have two UIViews. One UIView would have it's image property set to be the grey scale image and the other UIView would be set as usual the only difference is that you would position the initial UIView directly on top of the UIView containing the "normal" image.
I hope that is enough to push your idea a step further.