建议写成UIImage分类,如下:
.h
//变成圆形图片 - (UIImage *)circleImage;
.m
//变成圆形图片 - (UIImage *)circleImage { // NO代表透明 UIGraphicsBeginImageContextWithOptions(self.size, NO, 1); // 获得上下文 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; }
本文介绍了一个UIImage的分类方法,用于将图片转换为圆形。通过使用UIGraphicsBeginImageContextWithOptions、CGContextAddEllipseInRect和CGContextClip等函数,实现图片的圆形裁剪效果。
942

被折叠的 条评论
为什么被折叠?



