ios - 如何制作uiimageview圆。?

标签 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;
}

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.

最佳答案

像这个函数一样,最好使用 mask 来制作圆圈

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

关于ios - 如何制作uiimageview圆。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36832391/

相关文章:

ios - NSString 中的编码字符

ios - 正在调用performSelector,但未调用performSelector :afterDelay

swift - 为什么要将 self.imageView 分配给一个新常量?

ios - 如何在动画过程中检测 UIImageView 上的点击?

ios - 如何在 Xcode 5.1.1 或 Xcode 5 中获得与 iPhone 相同的 iPhone 模拟器 7.1 皮肤

ios - 设置后退按钮的名称和 View Controller 的标题

ios - 将用户操作发送到 iOS 中的父 uiview

ios - 如何在应用程序启动时只显示一次消息?

ios - 一起水平滚动 UICollectionView 的所有行

objective-c - iPhone : How to get the currency symbol from an amount string?