美文网首页
iOS开发 高性能的给 UIImageView 加个圆角

iOS开发 高性能的给 UIImageView 加个圆角

作者: 本本的开心牧场 | 来源:发表于2018-11-22 16:32 被阅读0次

日常开发中加圆角的便捷方式

//二行代码搞定
self.view.layer.cornerRadius = 5.0f;
self.view.layer.masksToBounds = YES;

但是这种方式会强制Core Animation提前渲染屏幕(离屏绘制), 而离屏绘制就会给性能带来负面影响,会有卡顿的现象出现。

解决方案:使用绘图
- (UIImage *)circleImage {
    // NO代表透明
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    // 获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 添加一个圆
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextAddEllipseInRect(ctx, rect);
    // 裁剪
    CGContextClip(ctx);
    // 将图片画上去
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭上下文
    UIGraphicsEndImageContext();
    return image;
}

相关文章

网友评论

      本文标题:iOS开发 高性能的给 UIImageView 加个圆角

      本文链接:https://www.haomeiwen.com/subject/lsglqqtx.html