如何制作uiimageview circle.?

时间:2016-04-25 05:07:24

标签: ios objective-c uiimageview cornerradius

我需要在半径范围内制作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;
}
  

它在iOS 5中的完美工作,但在其他设备上测试它的形状是变化我不知道它为什么会发生。请帮忙。

3 个答案:

答案 0 :(得分:1)

最好使用蒙版像这个功能一样制作圆圈

  - (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;
  }

答案 1 :(得分:0)

试试这个

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

答案 2 :(得分:0)

首先为equal width提供equal heightUIImageView,然后再使用

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

imgView.layer.masksToBounds = YES;
相关问题