I wonder why setting the corner radius of a ImageView will results in gray unwanted sharp angle, like this
Noted there is sharp gray angle behind the rounded corner image.
I set up the corner radius like this:
self.previewImageView.image = videoStream.thumbImage;
self.previewImageView.layer.cornerRadius = 8.0;
self.previewImageView.clipsToBounds = YES;
self.previewImageView.layer.masksToBounds = YES;
I also make sure the background color of the image view is white, but it wouldn't help anyway.
Anyone has any idea how to get rid of the sharp gray angle while setting the rounded corner of the image view?
I know I might draw a path and set the layer's mask path, are there any alternative?
My background view is a collection view cell, which happens to be not the same size as the imageView, i can't just set the corner radius of my background view
Try setting the image view's background color to clear.
just do:-
self.yourBackgroundView.layer.cornerRadius=self.yourImageView.layer.cornerRadius
You can try below code --
self.previewImageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]);
Hope this one is helpful.
Related
I'm trying to make a button that looks like this:
The button has
transparent background color
non-transparent title
semi-transparent white border color
What's the best way to do this? I know how to achieve the first two items, but how do I get the semi-transparent white border color?
Use the button's layer's properties:
yourButton.layer.borderWidth = 3.0f;
yourButton.layer.borderColor = [UIColor colorWithRed:178/255.0 green:170/255.0 blue:156/255.0 alpha:0.4].CGColor;
(Change the values so it looks good, alpha makes it semi-transparent)
Also, you'll need this if you don't have the circle yet:
yourButton.layer.cornerRadius = yourButton.frame.size.width/2;
EDIT: As #holex suggested, a better way to calculate the cornerRadius is:
CGFloat radius = MIN(yourButton.frame.size.width, yourButton.frame.size.height) / 2.0
yourButton.layer.cornerRadius = radius;
I'm using AVFoundation framework to scan a barcode, but that may be unrelevant for my problem.
What I want:
I would like that the square bordered in green be transparent (not with the darkened black).
Here is what I have done:
I have 2 views: backgroundView( which occupies the whole screen) and highlightView which is the square bordered with green, on top of backgroundView (I have used a XIB for dimensions and positions) :
self.highlightView.layer.borderColor = [UIColor greenColor].CGColor;
self.highlightView.layer.borderWidth = 3;
// this following line does not allow the square to be transparent
self.highlightView.layer.opacity = 0;
// relative to AVFoundation
_previewLayer.frame = _backgroundView.bounds;
[_backgroundView.layer addSublayer:_previewLayer];
_previewLayer.backgroundColor = [UIColor blackColor].CGColor;
_previewLayer.opacity = 0.3;
UPDATE : xib (here representing the square with a clear color background), the backgroundView has the property black color background).
As I mentioned, you were looking in the wrong direction. There are multiple posts with a problem similar to yours, which have pretty decent answers. (You will have to study and understand to make the most of them):
Cut Out Shape with Animation
Simply mask a UIView with a rectangle
To sum it up, you need to apply the semi-transparent color to the layer of backgroundView and then play around with the layer's mask property to get the work done.
You can find many tutorials to learn using the layer and mask together.
Hope this helps.
I converted an image to fully gray when loaded, but I want to remove the gray color from it when touch move and view original image color.
I want to know how to convert from gray color effect to the original image and from original to gray when user moves finger over the image.
My solution is the following.
You need TWO UIImageViews. Add one on the other and make them to overlap pixel by pixel. You have two options:
B&W in foreground
Color in foreground
Both solutions fundamentally are the same. You need to use a mask on the topmost view. With a mask layer the white areas will be fully opaque, blacks are 100% transparent, grays are semi-transparent.
Example for the mask:
CALayer *maskLayer = [CALayer layer];
UIImage *mask = [UIImage imageNamed:#"mask.png"];
maskLayer.contents = (id)mask.CGImage;
maskLayer.bounds = (CGRect){CGPointZero, mask.size};
UIImageView *viewToMask = ;
viewToMask.image = [UIImage imageNamed:#"blackandwhite.png"];
viewToMask.layer.mask = maskLayer;
[self.view addSubview:viewToMask];
Certainly you have to add/remove the mask on touch/release. I leave you with that.
Now what happens when you move your finger? Just move the mask to different positions like so:
viewToMask.layer.mask.position = offset position based on touch position
I hope I could help you.
I'm trying to make a simple view that is opaque except for a circle of a given diameter in the center. It is meant to overlay the camera, as they don't want the entire screen showing, simply that you center your face in the circle and snap the picture.
It's a few simple lines of code to to the opposite - a clear view with an opaque circle in the center - but I cannot figure out how to do the opposite.
Any help or pointers appreciated....
It sounds like you just want a simple mask. You can do that using Ash's method.
UIImage *_maskingImage = [UIImage imageNamed:#"mask"];
CALayer *_maskingLayer = [CALayer layer];
_maskingLayer.frame = theView.bounds;
[_maskingLayer setContents:(id)[_maskingImage CGImage]];
[theView.layer setMask:_maskingLayer];
You'll need an image that is a circle that has a transparency gradient going from black on the edge to transparent on the center. Remember which file type you're using as not all support transparency (like jpeg).
Alternatively, you could just use a UIImageView and have an image that has the gradient in it instead.
What about using the mask property of the view layer ?
Try something like this (the 2 views should have the same size) :
#import <QuartzCore/QuartzCore.h>
UIView *blackCircleOnClearBackground;
UIView *cameraView;
cameraView.layer.mask = blackCircleOnClearBackground.layer;
I want to add a corner radius to a UIButton. It is working fine but a problem occurs when I add image to it.
It does not round its corners with images, the image is shown in full rectangle form.
Please see the image, I have used the corner radius with red color and the output is as follow:
Please help.
Did you try to use set the masksToBounds: property? Fore example:
CALayer *layer = [myView layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:8.0];
That should do the trick.
you use -
myButton.imageView.layer.cornerRadius = 5;
but make sure that your image size is exact same as button size. its working for me.
yourButton.layer.cornerRadius = 10 //this value should be half of your button's height to make a circle
yourButton.clipsToBounds = true //this clips everything outside of bounds