iOS 制作圆形图片

@implementation UIImage (Extension)


/**

 * parm:sourceImage:需要剪切的原图片

 * parm:borderWidth:剪切后的边框宽度

 * parm:borderColor:边框颜色

 */


- (UIImage *)circleImage{

  return  [selfcircleImageWithImage:selfborderWidth:0borderColor:[UIColorclearColor]];

}



/**

 * parm:sourceImage:需要剪切的原图片

 * parm:borderWidth:剪切后的边框宽度

 * parm:borderColor:边框颜色

 */

- (UIImage *)circleImageWithImage:(UIImage *)sourceImage borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor{

   CGFloat imageWidth = sourceImage.size.width +2 * borderWidth;

   CGFloat imageHeight = sourceImage.size.height +2 * borderWidth;

   UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageWidth, imageHeight),NO, 0.0);

   UIGraphicsGetCurrentContext();

   CGFloat radius = (sourceImage.size.width < sourceImage.size.height?sourceImage.size.width:sourceImage.size.height)*0.5;

   UIBezierPath *bezierPath = [UIBezierPathbezierPathWithArcCenter:CGPointMake(imageWidth *0.5, imageHeight * 0.5)radius:radius startAngle:0endAngle:M_PI *2 clockwise:YES];

    bezierPath.lineWidth = borderWidth;

    [borderColorsetStroke];

    [bezierPathstroke];

    [bezierPathaddClip];

    [sourceImagedrawInRect:CGRectMake(borderWidth, borderWidth, sourceImage.size.width, sourceImage.size.height)];

   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();

   return image;

}



@implementation UIImageView (Extension)

- (void)setHeader:(NSString *)url

{

   UIImage *placeholder = [[UIImageimageNamed:@"defaultimg"]circleImage];

   self.contentMode =UIViewContentModeScaleAspectFill;

    [selfsd_setImageWithURL:[NSURLURLWithString:url] placeholderImage:placeholdercompleted:^(UIImage *image,NSError *error, SDImageCacheType cacheType,NSURL *imageURL) {

       self.image = image ? [imagecircleImage] : placeholder;

    }];

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值