如何使uiimageview圈。?

How to make uiimageview circle.?

我需要在圆半径内制作 UIImageView,我为此任务使用了这段代码。

-(void)viewWillAppear:(BOOL)animated {

    [self performSelector:@selector(setStyleCircleForImage:) withObject:_imageview afterDelay:0];

    [super viewWillAppear:YES];
}

-(void) setStyleCircleForImage:(UIImageView *)imgView {

    imgView.layer.cornerRadius = _imageview.frame.size.height / 2.0;
    imgView.clipsToBounds = YES;
}

Its working perfect in iOS 5 but when test in other device it shape is change I don't know why it happens. Please make some help.

试试这个

 yourImageView.layer.cornerRadius = yourImageView.frame.size.width/2;
 yourImageView.layer.masksToBounds = YES;

像这个函数一样用蒙版画圆比较好

  - (void)viewDidLoad {
    [super viewDidLoad];
    [self makeCircleImage: _imageview];
 }

  -(void)makeCircleImage:(UIImageView *)img{
      CGFloat img_width = img.bounds.size.width;
      UIGraphicsBeginImageContextWithOptions(CGSizeMake(img_width,img_width), NO, 0);
      CGContextRef c = UIGraphicsGetCurrentContext();
      CGContextSetFillColorWithColor(c, [UIColor blackColor].CGColor);
      CGContextFillEllipseInRect(c, CGRectMake(0,0,img_width,img_width));
      UIImage* maskim = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();

      CALayer* mask = [CALayer new];
      mask.frame = CGRectMake(0,0,img_width,img_width);
      mask.contents = (id)maskim.CGImage;

      img.layer.mask = mask;
  }

先给equal widthequal heightUIImageView,然后利用

imgView.layer.cornerRadius = imgView.frame.size.height / 2.0;

imgView.layer.masksToBounds = YES;