Gypsa
Gypsa

Reputation: 11314

Image based core graphics drawing

I have to draw many shapes like rectangle, circle, triangle etc.I am drawing them trough core graphics. It is working fine. I want to ask suppose I am having an image (square image of say 1024*1024), and now I want my circle to draw as image means like I draw red color circle or other color, so if it is possible to show my image as drawn in the circle, like the image is circle,

My code for normal circle  drawing:-
- (void)drawRect:(CGRect)rect{

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, [ [UIColor colorWithRed:255.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1] CGColor]);
    CGContextSetAlpha(ctx, 0.7);
    CGContextMoveToPoint(ctx, radius, radius);
    CGContextAddArc(ctx, radius,radius, radius,  radians(0), radians(360), 0);
    CGContextClosePath(ctx);
    CGContextFillPath(ctx);

}

I want to draw my image as circle.

Basically I want like we can fill colors in core graphics drawing whether circle, arc, triangle. so is that possible to fill my shape with my image instead of any solid colors.

Is this possible and if yes please suggest me how to achieve this. Thanks in advance.

Upvotes: 2

Views: 278

Answers (2)

The iOSDev
The iOSDev

Reputation: 5267

try this out for setting color in CGContextSetFillColorWithColor function [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"pattern.jpg"]CGColor];

Upvotes: 1

Simone Pistecchia
Simone Pistecchia

Reputation: 2832

Add a UIView with and rectangular on the image.

In this view, you must draw the hole.

Now i not have the mac and i cannot check, but try to substitute

CGContextSetFillColorWithColor(ctx, [ [UIColor colorWithRed:255.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1] CGColor]);

with

CGContextSetBlendMode(currentContext, kCGBlendModeClear);

let me know

Upvotes: 0

Related Questions