Three methods for setting rounded corners for iOS and three methods for ios rounded corners

Source: Internet
Author: User

Three methods for setting rounded corners for iOS and three methods for ios rounded corners

Method 1: Set layer attributes.

This is the simplest one, but it affects performance. It is rarely used in normal development.

?
1234567 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];// You only need to set two attributes of the layer.// Set the rounded cornerimageView.layer.cornerRadius = imageView.frame.size.width / 2;// Cut out the excess partsimageView.layer.masksToBounds = YES;[self.view addSubview:imageView];

Method 2: Use the UIBezierPath and Core Graphics framework of the besell curve to draw a rounded corner.

?
1234567891011 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; imageView.image = [UIImage imageNamed:@"1"]; // Start drawing the imageView UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0); // Draw a circular chart using the besell Curve [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip]; [imageView drawRect:imageView.bounds]; imageView.image = UIGraphicsGetImageFromCurrentImageContext();  // End drawing UIGraphicsEndImageContext(); [self.view addSubview:imageView];

 

Method 3: Use CAShapeLayer and UIBezierPath to set the rounded corner.

#import "ViewController.h"#import <AVFoundation/AVFoundation.h>@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {        [super viewDidLoad];        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];    imageView.image = [UIImage imageNamed:@"1"];    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(25, 5)];    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];    maskLayer.frame = imageView.bounds;    maskLayer.path = maskPath.CGPath;    imageView.layer.mask = maskLayer;    [self.view addSubview:imageView];}

Among the three methods, the third is the best, with the least memory consumption and fast rendering.

:

 

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.