美文网首页
OC-UIImageView优化圆角,防止离屏渲染

OC-UIImageView优化圆角,防止离屏渲染

作者: 紫云夕月 | 来源:发表于2021-08-05 09:49 被阅读0次

先使用Core Graphics自己画出了一个圆角矩形图片,再设置边框宽度和颜色,也不会触发离屏渲染

iconImage = [UIImage createRoundedRectImage:self.iconImage size:CGSizeMake(100, 50) radius:25];
iconView = [[UIImageView alloc] initWithImage:iconImage];
iconView.layer.borderWidth = 2.0;
iconView.layer.borderColor = UIColor.redColor.CGColor;
iconView.frame = CGRectMake(20, 5, 100, 50);
iconView.layer.cornerRadius = 25;
iconView.layer.masksToBounds = YES;
[self.view addSubview:iconView];
#import <UIKit/UIKit.h>

@interface UIImage (DSRoundImage)

+ (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(int)radius;

@end
#import "UIImage+DSRoundImage.h"

@implementation UIImage (DSRoundImage)

static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight)
{
    float fw, fh;
    
    if (ovalWidth == 0 || ovalHeight == 0)
    {
        CGContextAddRect(context, rect);
        return;
    }
    
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGContextScaleCTM(context, ovalWidth, ovalHeight);
    fw = CGRectGetWidth(rect) / ovalWidth;
    fh = CGRectGetHeight(rect) / ovalHeight;
    
    CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right
    
    CGContextClosePath(context);
    CGContextRestoreGState(context);
}

+ (id)createRoundedRectImage:(UIImage *)image size:(CGSize)size radius:(int)radius{
    
    size = CGSizeMake(size.width*2, size.height*2);
    radius = radius*2;
    
    UIImage * img = image;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 4 * size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    CGContextBeginPath(context);
    addRoundedRectToPath(context, rect, radius, radius);
    CGContextClosePath(context);
    CGContextClip(context);
    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), img.CGImage);
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    img = [UIImage imageWithCGImage:imageMasked];
    
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageMasked);
    return img;
}

@end

相关文章

  • OC-UIImageView优化圆角,防止离屏渲染

    先使用Core Graphics自己画出了一个圆角矩形图片,再设置边框宽度和颜色,也不会触发离屏渲染

  • UILabel 圆角

    很多人发现这样设置后圆角不生效,必须加上下面代码才会有圆角 但是这样做就会触发离屏渲染,可直接这样做防止离屏渲染 ...

  • 2018-07-04

    后台绘制圆角图片 参考链接链接 iOS 离屏渲染优化(Offscreen Render)

  • CALayer属性及优化

    iOS性能优化中的离屏渲染 场景:当使用圆角,阴影,遮罩的时候 为什么离屏渲染会造成性能消耗: 屏幕外渲染并不意味...

  • 离屏渲染

    一、离屏渲染的产生的原因 ?????? 待补充 二、离屏渲染的解决方法 1. 圆角优化 使用贝塞尔曲线 UIBez...

  • iOS圆角的离屏渲染,你真的弄明白了吗

    iOS圆角的离屏渲染,你真的弄明白了吗iOS圆角的离屏渲染,你真的弄明白了吗

  • iOS 设置圆角会造成离屏渲染,你真的弄明白了吗?

    1. 如何设置圆角才会触发离屏渲染 我们经常看到,圆角会触发离屏渲染。但其实这个说法是不准确的,因为圆角触发离屏渲...

  • 性能优化的点

    1 离屏渲染 光栅化 圆角(生成圆角图 系统圆角 贝塞尔曲线遮罩) 后两个会离屏渲染 阴影 加上showdowPa...

  • Offscreen Rendering

    Offscreen Rendering(离屏渲染)概念理解: 离屏渲染 是指系统为了绘制圆角(cornerRadi...

  • iOS 圆角阴影优化

    目录 离屏渲染原理 iOS添加圆角的方法比较 圆角和阴影怎么同时存在 autoLayout下怎么使用 1.离屏渲染...

网友评论

      本文标题:OC-UIImageView优化圆角,防止离屏渲染

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