This is the code I have:
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0].CGColor;
CGRect rectangle = CGRectMake(100, 100, 100, 100);
CGRect inside = CGRectMake(120, 120, 60, 60);
CGContextSetFillColorWithColor(context, redColor);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextFillRect(context, rectangle);
CGContextSaveGState(context);
CGContextSetBlendMode(context, kCGBlendModeSourceOut);
CGContextFillRect(context, inside);
CGContextRestoreGState(context);
I’m trying to make the inside shape hollow, but instead it’s giving me a red square (as expecetd) with a black square in the middle, when the UIView in the background is white.
Any ideas on how to make the shape transparent here?
There are a few ways you can fill your rectangle with a hollow inner rectangle. One way is to create a path that contains both rectangles, and fill it using the even-odd rule.