Common ways to set rounded corners in iOS

Source: Internet
Author: User

First approach: The most common approach, but with the worst performance

Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (100, 100, 100, 100)];

Imageview.image = [UIImage imagenamed:@ "late ant. jpg"];

You only need to set the layer's two properties

Set rounded corners

ImageView.layer.cornerRadius = IMAGEVIEW.FRAME.SIZE.WIDTH/2;

Cut out the extra parts

ImageView.layer.masksToBounds = YES;

[Self.view Addsubview:imageview];

The second method: using Bezier Uibezierpath and the core graphics frame to draw a rounded corner, the performance is much higher than the first (some people say 100 times, specifically how much I do not know)

Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (100, 100, 100, 100)];

Imageview.image = [UIImage imagenamed:@ "late ant. jpg"];

Start drawing on ImageView

Uigraphicsbeginimagecontextwithoptions (ImageView.bounds.size, NO, 1.0);

Draw a circular chart using Bezier curves

[[Uibezierpath BezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addclip];

[ImageView DrawRect:imageView.bounds];

Imageview.image = Uigraphicsgetimagefromcurrentimagecontext ();

Finish drawing

Uigraphicsendimagecontext ();

[Self.view Addsubview:imageview];

Third method: Use Cashapelayer and uibezierpath to set rounded corners

First you need to import <AVFoundation/AVFoundation.h>

The memory consumption is minimal, and the rendering is fast.

Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (100, 100, 100, 100)];

Imageview.image = [UIImage imagenamed:@ "late ant. jpg"];

Uibezierpath *maskpath = [Uibezierpath bezierPathWithRoundedRect:imageView.bounds byroundingcorners: Uirectcornerallcorners CornerRadii:imageView.bounds.size];

Cashapelayer *masklayer = [[Cashapelayer alloc]init];

Set size

Masklayer.frame = Imageview.bounds;

Set graphic appearance

Masklayer.path = Maskpath.cgpath;

ImageView.layer.mask = Masklayer;

[Self.view Addsubview:imageview];

Common ways to set rounded corners in iOS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.