Coder Social home page Coder Social logo

jmroundedcorner's Introduction

JMRoundedCorner

LICENSE  CocoaPods  SUPPORT  BLOG 

iOS 9.0 之后 UIButton 设置圆角会触发离屏渲染,而 UIImageView 里 png 图片设置圆角不会触发离屏渲染了,所以你还在等什么,上 iOS 9 吧。

避免离屏渲染

如果你的 view 不需要让子视图超出部分不显示,且不需要给 view 的 image 绘制圆角,可以查看 cornerRadius 属性的注释:

By default, the corner radius does not apply to the image in the layer’s contents property; it applies only to the background color and border of the layer. However, setting the masksToBounds property to true causes the content to be clipped to the rounded corners.

这个属性会影响 layer 的背景颜色和 border,所以如下代码即可避免离屏渲染。

view.layer.cornerRadius = radius;
view.layer.backgroundColor = backgroundColor.CGColor;

使用

在工程的 Podfile 文件中添加如下行:

pod 'JMRoundedCorner', :git => 'https://github.com/raozhizhen/JMRoundedCorner.git', :tag => '1.9.6'
pod 'YYWebImage', :git => 'https://github.com/raozhizhen/YYWebImage.git', :tag => '1.0.5'

在代码中引入:

#import <JMRoundedCorner/JMRoundedCorner.h>

代码示例

[_avatarView jm_setImageWithCornerRadius:10 image:[UIImage imageNamed:@"avatar"]];
// or
[_avatarView jm_setImageWithCornerRadius:20 imageURL:_avatarURL placeholder:@"avatar" size:CGSizeMake(40, 40)];

支持通过 JMRadius 设置4个角为不同的弧度,角度优先级为左上 > 右上 > 左下 > 右下:

[_avatarView jm_setImageWithJMRadius:JMRadiusMake(20, 20, 20, 20)
                            imageURL:_avatarURL
                         placeholder:@"avatar"
                         borderColor:[UIColor redColor]
                         borderWidth:1
                     backgroundColor:[UIColor blueColor]
                         contentMode:UIViewContentModeScaleAspectFill
                                size:CGSizeMake(40, 40)];

性能上的优缺点

  • 优点:没有了离屏渲染,调整了 image 的像素大小以避免不必要的缩放;
  • 缺点:会造成图层混合,且因为只是绘制了一个带圆角的图片,所以不能使子视图超出圆角部分不显示。

注意事项

内存会持续提升,是正常现象,点击 home 键内存会回到正常水平,并非内存泄漏,只是绘制的缓存,在内存不足时会自动释放。

不要设置 view 的 backgroundColor,需要设置的话可以通过带 backgroundColor 参数的接口进行设置,例如:

- (void)jm_setImageWithCornerRadius:(CGFloat)radius
                        borderColor:(UIColor *)borderColor
                        borderWidth:(CGFloat)borderWidth
                    backgroundColor:(UIColor *)backgroundColor;

控制器输出以下错误,这是 Xcode 7 的 BUG

<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

更新日志

  • 2016/10/10 1.9.0 版本: 依赖 YYWebImage 实现网络图片圆角处理和圆角图片缓存。
  • 2016/4/25 1.2.1 版本: 使用 NSOperationQueue 代替 dispatch_queue,当重复设置圆角的时候会自动 cancel 上一次操作,感谢 kudocc 的 pull request。
  • 2016/3/12 1.1.0 版本: 接口带上了 jm_ 前缀,JMRadius 添加圆角优先级。
  • 2016/3/3 1.0.3 版本: 修复 label 里如果没有汉字,文字就不显示的 BUG,以及做了使 view 落在像素点上的优化。
  • 2016/2/28 1.0.0 版本:发布正式版本。
  • 2016/2/26 0.0.4 版本:去掉了 size 参数及支持 JMRadius 设置4个角为不同的弧度。
  • 2016/2/25 0.0.3 版本:去掉了 UIImageView 这个中间控件。
  • 2016/2/24 0.0.2 版本:支持设置背景图片的绘制模式(cotentmode)。
  • 2016/2/23 0.0.1 版本:绘制一个圆角 image。

感谢

联系我

许可证

JMRoundedCorner 使用 MIT 许可证,详情见 LICENSE 文件。

jmroundedcorner's People

Contributors

cyhsutw avatar kudocc avatar raozhizhen avatar xiaofei86 avatar yimingtang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jmroundedcorner's Issues

主线程卡死

退出后台,进入前台,然后执行 jm_setJMRadius。。。

这句话会导致整个界面卡死 , 很严重
dispatch_sync(dispatch_get_main_queue(), ^{
_size = wself.bounds.size;
});

UIButton 没法给不同的state状态设置圆角

RT
使用"UIView+RoundedCorner.h"无法直接给UIButton设置圆角
库里只给normal state设置了圆角
highled和disable状态没有设置

只能先生成一个圆角图片,再设置给button

extern

发现pixel函数写成extern了,哈哈,作者乘机改成内联的吧~~

接口不太好用

比如:
1.只设置圆角
2.只设置左上方的圆角
类似的,我要调用- (void)jm_setCornerRadius:(CGFloat)radius withBackgroundColor:(UIColor *)backgroundColor;
backgroundColor没啥用,或者可以增加个- (void)jm_setCornerRadius:(CGFloat)radius;?
还有.h文件的接口排列有点乱,谢谢!

各种告警错误

Sep 13 16:53:16 iphoneLive[4177] : CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep 13 16:53:16 iphoneLive[4177] : CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep 13 16:53:16 iphoneLive[4177] : CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep 13 16:53:16 iphoneLive[4177] : CGContextMoveToPoint: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep 13 16:53:16 iphoneLive[4177] : CGContextAddArcToPoint: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep 13 16:53:16 iphoneLive[4177] : CGContextAddArcToPoint: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep 13 16:53:16 iphoneLive[4177] : CGContextAddArcToPoint: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep 13 16:53:16 iphoneLive[4177] : CGContextAddArcToPoint: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep 13 16:53:16 iphoneLive[4177] : CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

框架异常

异常消息,图像不能正常显示:
CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

内存泄露

[self.view jm_setJMRadius:JMRadiusMake(0, 10, 0, 10) withBackgroundColor:[UIColor colorWithRed:0.918 green:0.533 blue:0.463 alpha:1.000]];

上面的代码

view 不释放

循环引用问题

  • (void)jm_setJMRadius:(JMRadius)radius withBorderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth backgroundColor:(UIColor *)backgroundColor backgroundImage:(UIImage *)backgroundImage contentMode:(UIViewContentMode)contentMode size:(CGSize)size

这个方法中在block中嵌套了block, 内存会出问题。

检查下吧

UILabel 设置不规则圆角的时候,方向会不对

比如 radius = 5, 8,2,3

最后裁剪出来的效果是 5,8 在下端。并不是期望的是在上端,我看下了代码,裁剪后的图片是对的,但是在给label 设置 layer.backgroundcolor的时候,label 自己转了下。

无法裁剪背景色

如果我UIView或者UIButton设置了背景色,[view setBackgroundColor:[UIColor blueColor]],这样圆角之后,底色还是背景色。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.