Reputation: 227
I have a UIImageView and i'd like to make a circular region in the centre of the image view transparent so that when an image is passed to the image view, the image seems to cover all of the image view except for the centre.
Here's what i currently have Instead of that black circle in the image, I want to see the background.
There is a similar question here but I need someone to show me how to do it in code as I don't really know Core Graphics.
Any help would be greatly appreciated.
Upvotes: 2
Views: 363
Reputation: 1
Swift 5
let maskPath = UIBezierPath(ovalIn: albumImageView.bounds)
let holePath = UIBezierPath(ovalIn: albumImageView.bounds.insetBy(dx: 30,dy: 30))
maskPath.append(holePath)
let mask = CAShapeLayer()
mask.fillRule = .evenOdd
mask.path = maskPath.cgPath
albumImageView.layer.mask = mask
Upvotes: 0
Reputation: 3261
This will do:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 50.0, 100.0, 100.0)];
[imageView setBackgroundColor:[UIColor redColor]];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithOvalInRect:imageView.bounds];
UIBezierPath *holePath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(imageView.bounds, 35.0, 35.0)];
[maskPath appendPath:holePath];
CAShapeLayer *mask = [CAShapeLayer layer];
[mask setFillRule:kCAFillRuleEvenOdd];
mask.path = maskPath.CGPath;
[imageView.layer setMask:mask];
[self.view addSubview:imageView];
The more inset you give the 'holePath' the smaller the radius of the inner circle ;)
Upvotes: 4