Is it possible to transform a UIView in a circle image? - ios

I have downloaded the new Facebook messanger App for iOS. I was wondering, is there some option that allows to "crop" an image and leave only a circle?
Would be great to be able to put a UIImage which is rectangular and crop the circular part.
Or do you think this is done server-side? In other words, there is no special iOS cropping function but simply a cropping software on the Facebook server?

use
imageView.layer.cornerRadius=imageView.frame.size.width/2.0;
imageView.clipsToBounds=YES;

This is actually quite easy to do.
What you want to do is to create a CAShapeLayer the same size as your view. Create a UIBezierPath that uses a rounded rectangle who's corner radius is 1/2 the height/width. That gives you a circular path.
Install the bezier path's CGPath into the shape layer. Then set the shape layer to fill with an opaque color.
Finally, install the shape layer as the mask on your view's layer. The result is that the shape layer clips the view and only shows the opaque parts of the shape layer.

Related

What does normalizedPath refer to and how can I draw it over an image in iOS?

I have two images, one that is a monochrome one which is a mask and another one with full color. What I need to do is find the CGRect of the mask (white pixels) in the other full color one.
What I did is to first find the contour of the mask using the Vision framework. Now, this returns a CGPath which is normalised. How can I translate this path into coordinates to the other image? Both have been scaled the same way to make them the same size so the translation should be "easy" but I can't figure it out.

Clip Drawing to Opaque (Stroke) Part of CAShapeLayer

I have a CAShapeLayer with an opaque stroke and a transparent fill. I then want to call myContext.drawRadialGradient, but have this radial gradient clip to the stroke of my shape layer. Currently, I'm calling myShapeLayer.path.addClip(), which clips the radial gradient to the fill region of the shape layer, instead of the stroke.
In other words, I'd like to use either just the opaque part of a layer or just the stroke (same thing in my case), and use it to clip the current context. I've been searching the CoreGraphics docs for a while to no avail.
Found my answer. You can create a new UIBezierPath that is a copy of your existing one, with a stroke, as below:
let strokePath = UIBezierPath(cgPath: oldPath.cgPath.copy(
strokingWithWidth: ..., lineCap: ..., lineJoin: ..., miterLimit: ...))
strokePath.addClip()
What if you add your shape layer as the mask of your gradient layer? That should work, since that's what a mask layer is supposed to do.
If that doesn't work you might need to capture your shape layer to an image and install THAT as the mask of your gradient layer.
Convert the stroked path to a new path whose fill area is the same as the original stroke. Now you have a fillable area that you can use as clipping area. The CGContext way to do this is by calling replacePathWithStrokedPath. As the docs explicitly say:
For example, you can clip to the stroked version of a path by calling this function followed by a call to the function clip().

Create custom shape using Bezier Curve in iOS

I have one requirement to show the a shape (like Home or Car etc) and depending upon the percentage of the condition will be filled with animation
I was trying with image but when I googled, I found this can be achieved by drawing image using Bezier curve.
So this is the image I am trying to draw and there are other images too.
So here are my two questions :
Can I achieve this using UIImage directly.
If not then how can i draw bezier
curve for such images. How will I calculate the points.

How can I apply a gradient blur effect on a view?

I have drawn a path on a view, All I need is to apply a gradient blur effect from the outer edge of the closed drawn path to the view's bounds. My question is mainly about the Gradient blur effect rather than applying it on the path.
I've not tried this but believe you could make it work:
create the path
use the path as a mask can create an new image that is clear outside the path while selecting the interior of the path (the interior has to be opaque)
use a radial blur on the whole image
composite the saved interior path OVER the blurred image
stroke the path to get a clear definition between the original interior and the blurred exterior

Best way to display rounded corner image with sprite

I need to display image with rounded corner with sprite. What is the best way to do this.
I explained how to do it in your other post ...
How to clip the texture in rounded shape
If you mean of smoothing the corners of the image you're drawing, there is no way to do with the normal sprite interface (other than modifying the original image to be smoothed beforehand)
To do it you must work with shaders.

Resources