zoukankan      html  css  js  c++  java
  • 如何高性能的给UIImageView加个圆角?

    https://www.jianshu.com/p/44bbff4274f3

    由于这样处理的渲染机制是GPU在当前屏幕缓冲区外新开辟一个渲染缓冲区进行工作,也就是离屏渲染,这会给我们带来额外的性能损耗,如果这样的圆角操作达到一定数量,会触发缓冲区的频繁合并和上下文的的频繁切换,性能的代价会宏观地表现在用户体验上----掉帧。
    会引发离屏渲染的操作:

    The following will trigger offscreen rendering:
    Any layer with a mask (layer.mask)
    Any layer with layer.masksToBounds / view.clipsToBounds being true
    Any layer with layer.allowsGroupOpacity set to YES and layer.opacity is less than 1.0
    Any layer with a drop shadow (layer.shadow*).
    Any layer with layer.shouldRasterize being true
    Any layer with layer.cornerRadius, layer.edgeAntialiasingMask, layer.allowsEdgeAntialiasing
    Text (any kind, including UILabel, CATextLayer, Core Text, etc).
    Most of the drawing you do with CGContext in drawRect:. Even an empty implementation will be rendered offscreen.

    因为这些效果均被认为不能直接呈现于屏幕,而需要在别的地方做额外的处理预合成。具体的检测我们可以使用Instruments的CoreAnimation。

    /** * @brief clip the cornerRadius with image, UIImageView must be setFrame before, no off-screen-rendered */

    - (void)zy_cornerRadiusWithRoundedRect:(CGRect)rect image:(UIImage *)image cornerRadius:(CGFloat)cornerRadius rectCornerType:(UIRectCorner)rectCornerType {

        CGSize size = self.bounds.size;

        CGFloat scale = [UIScreen mainScreen].scale;

        CGSize cornerRadii = CGSizeMake(cornerRadius, cornerRadius);

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            UIGraphicsBeginImageContextWithOptions(size, NO, scale);

            if (nil == UIGraphicsGetCurrentContext()) {

                return;

            }

            UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:rectCornerType cornerRadii:cornerRadii];

            [cornerPath addClip];

            [image drawInRect:rect];

            id processedImageRef =  (__bridge id _Nullable)(UIGraphicsGetImageFromCurrentImageContext().CGImage);

            UIGraphicsEndImageContext();

    //        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    //        UIImage *newImage = [UIImage imageWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage];

            dispatch_async(dispatch_get_main_queue(), ^{

                self.layer.contents = processedImageRef;

    //            self.image = newImage;

            });

        });

    }

  • 相关阅读:
    使用jquery插件validate制作的表单验证案例
    POJ2992:Divisors(求N!因子的个数,乘性函数,分解n!的质因子(算是找规律))
    HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题
    HDU4135Co-prime(容斥原理)
    HDU1796How many integers can you find(容斥原理)
    Miller-Rabin素数测试算法(POJ1811Prime Test)
    乘法逆元+模的运算规则
    因子和与因子个数 (乘性函数)
    费马小定理的证明:
    整数(质因子)分解(Pollard rho大整数分解)
  • 原文地址:https://www.cnblogs.com/dengchaojie/p/8855394.html
Copyright © 2011-2022 走看看