我需要在圆半径内制作 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/