UIImageView添加圆角

  • 最直接的方法就是使用如下属性设置:

1

2

3

imgView.layer.cornerRadius = 10;

// 这一行代码是很消耗性能的

imgView.clipsToBounds = YES;

**这是离屏渲染(off-screen-rendering),消耗性能的**

  • 给UIImage添加生成圆角图片的扩展API:这是on-screen-rendering

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

- (UIImage *)imageWithCornerRadius:(CGFloat)radius {

CGRect rect = (CGRect){0.f, 0.f, self.size};

UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale);

CGContextAddPath(UIGraphicsGetCurrentContext(),

 [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);

CGContextClip(UIGraphicsGetCurrentContext());

[self drawInRect:rect];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

时间: 2024-08-24 20:05:19

UIImageView添加圆角的相关文章

iOS 高效添加圆角效果实战讲解

圆角(RounderCorner)是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受.但很多人并不清楚如何设置圆角的正确方式和原理.设置圆角会带来一定的性能损耗,如何提高性能是另一个需要重点讨论的话题.我查阅了一些现有的资料,收获良多的同时也发现了一些误导人错误.本文总结整理了一些知识点,概括如下: 设置圆角的正确姿势及其原理 设置圆角的性能损耗 其他设置圆角的方法,以及最优选择 我为本文制作了一个 demo,读者可以在我的 github 上 clone 下来:CornerRadius

为 UIImageView 添加Tap手势

// // ZJD_TapImageView.h // V6 // // Created by aidong on 15/8/18. // Copyright (c) 2015年 aidong. All rights reserved. // /** * 为UIImageView添加tap手势. * 用法: ZJD_TapImageView *iv = [[ZJD_TapImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; iv.

为input输入框添加圆角并去除阴影

<input type="text" name="bianhao" value="" placeholder="请输入商品编号" maxlength="10" size="10" style="width:105px; margin:0px 0px 0px 12px;height:22px;border-radius:4px; border:1px solid #DBDB

[iOS]UIImageView增加圆角

[iOS]UIImageView增加圆角 "如何给一个UIImageView增加圆角?有几种方法?各自区别?" 备注:本文参考自http://www.jianshu.com/p/d1954c9a4426 UIImageView *poImgView = [[UIImageView alloc]init]; 方案A(基本方案): poImgView.layer.cornerRadius = poImgView.frame.size.width/2.0; poImgView.layer.m

为Input输入框添加圆角并去除阴影 去除获得焦点时方角样式

目录 1.为Input输入框添加圆角并去除阴影 2.去除获得焦点时方角样式 内容 1.为Input输入框添加圆角并去除阴影 1.1 border-radius不同浏览器下兼容设置: .boder-radius{ -webkit-border-radius:6px;//适配以webkit为核心的浏览器(chrome.safari等) -moz-border-radius:6px;//适配firefox浏览器 -ms-border-radius:6px;//适配IE浏览器 -o-border-rad

iOS给UIimage添加圆角的两种方式

众所周知,给图片添加圆角有CALayer的cornerRadius, 比如: 最直接的方法: imgView.layer.cornerRadius1=110; imgView.clipsToBounds = YES; 这事离屏渲染 (off - screen - rendering), 是很消耗性能的:有很多公司面试的时候会问到,你怎么将图片设置圆角,如果你 只回答了这个方法,那么很遗憾,没有加分. 下面我介绍一种更好的方法: #import "Bys.h" @implementati

UWP Button添加圆角阴影(三)

原文:UWP Button添加圆角阴影(三) Composition DropShadow是CompositionAPI中的东西,使用Storyboard设置某个属性,就是频繁的触发put_xxx()方法,效率远远不如使用CompositionAnimation. Composition对象的基类CompositionObject拥有一个属性叫ImplicitAnimations,可以通过他实现累死css的transition的效果,也就是对应属性修改的时候,平滑的过渡过去. 可以从DropSh

UWP Button添加圆角阴影(二)

原文:UWP Button添加圆角阴影(二) 阴影 对于阴影呢,WindowsCommunityToolkit中已经有封装好的DropShadowPanel啦,只要引用Microsoft.Toolkit.Uwp.UI.Controls这个Nuget包就可以使用啦. 直接把阴影套在咱们的圆角Button外面呢,会出现圆角的Button映出直角的阴影的丑陋状况.对于这种情况肯定是有处理方式的. 看DropShadowPanel的源码,对Content的特殊类型做了处理.下面我详细的说下. Compo

吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:为图片添加圆角 (IE8 不支持)

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"