通过绘制上下文的方式绘制图片圆角

    xiaoxiao2025-10-25  11

    </pre><p></p><p></p><p>//通过绘图的方式给图片设置圆角,而view的‘layer.cornerRadius’方法是离屏渲染,很消耗内存,所以可以通过下面的方法给图片设置圆角</p><p>//为imageView 添加类别,给imageView的图片设置圆角</p><pre name="code" class="objc">-(void)imageWithCornerRadius:(CGFloat)radius { UIGraphicsBeginImageContextWithOptions(self.frame.size,NO, UIScreen.mainScreen.scale); CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) cornerRadius:radius].CGPath); CGContextClip(UIGraphicsGetCurrentContext()); [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; self.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); }

    //为UIImage添加生成圆角的API方法

    -(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; }
    转载请注明原文地址: https://ju.6miu.com/read-1303503.html
    最新回复(0)