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;

    }];

}




你可能感兴趣的:(iOS,iOS,制作圆形图片)