mm24
mm24

Reputation: 9596

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

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?

This is the result I would like to achieve.

Would be great to be able to put a UIImage which is rectangular and crop the circular part.

enter image description here

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?

Upvotes: 1

Views: 731

Answers (2)

santhu
santhu

Reputation: 4792

use

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

Upvotes: 4

Duncan C
Duncan C

Reputation: 131426

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.

Upvotes: 0

Related Questions