【问题标题】:How to make uiimageview circle.?如何使uiimageview圆圈。?
【发布时间】:2016-04-25 05:07:23
【问题描述】:

我需要在圆半径内制作 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 中运行完美,但在其他设备上测试时,它的形状发生了变化,我不知道为什么会发生这种情况。请帮忙。

【问题讨论】:

  • 两个规则 1.组件的宽度和高度应该相同。 2.在你的情况下,框架可能没有更新,尝试输入viewWillLayoutSubviews它应该可以工作。
  • 好的先生,我尝试实现它
  • imgView.layer.cornerRadius = imgView.frame.size.width / 2.0;试试这个
  • @iphonic 如果我使 uiimageview 的宽度和高度相同,但在其他设备上却不同
  • 你的代码有问题

标签: ios objective-c uiimageview cornerradius


【解决方案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;
  }

【讨论】:

  • 我将如何关联我的图像名称这种方法?
  • 是的,先生,它在 iPhone 6s 上工作,让我检查一些其他设备。
【解决方案2】:

试试这个

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

【讨论】:

  • 是的。您必须添加 viewDidLoad。你为什么使用选择器?
  • @sandeeptomar 你必须使用masksToBounds insted of clipsToBounds
  • 好的,先生,您的代码中的 _imgFriendsPhoto 是什么。会按图片名称吗?
  • @sandeeptomar 你试过了吗?
  • 是的,先生,但它创造了同样的东西,每当我在 iPhone 5 上测试它工作但如果我测试一些其他设备,它不会出现圆圈
【解决方案3】:

先给equal widthequal height换成UIImageView,然后使用

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

imgView.layer.masksToBounds = YES;

【讨论】:

  • 我通过EI队长的回答解决了我的问题。谢谢楼主支持
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 2019-07-19
  • 1970-01-01
  • 2021-10-15
相关资源
最近更新 更多