代码改变世界

一个简单的创建圆角图像的UIImage扩展实现

2014-11-10 13:10  JG2014  阅读(262)  评论(0编辑  收藏  举报

- (UIImage *)roundedCornerImageWithCornerRadius:(CGFloat)cornerRadius { CGFloat w = self.size.width; CGFloat h = self.size.height; CGFloat scale = [UIScreen mainScreen].scale; // 防止圆角半径小于0,或者大于宽/高中较小值的一半。 if (cornerRadius < 0) cornerRadius = 0; else if (cornerRadius > MIN(w, h)) cornerRadius = MIN(w, h) / 2.; UIImage *image = nil; CGRect imageFrame = CGRectMake(0., 0., w, h); UIGraphicsBeginImageContextWithOptions(self.size, NO, scale); [[UIBezierPath bezierPathWithRoundedRect:imageFrame cornerRadius:cornerRadius] addClip]; [self drawInRect:imageFrame]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }