sregorcinimod
sregorcinimod

Reputation: 277

Draw a polygon and fill it with a background image

I am trying to draw a polygon with four points (A,B) (C,D) (E,F) (G,H) therefore not necessarily a rectangle.

I then want to apply a patterned image on to the polygon.

I have looked into initWithPatternImage, colorWithPatternImage, UIBezierPath, CGContextStrokeLineSegments but can't figure out how to put it all together.

Anybody got any ideas of how you could put them all together?

NOTE: I'm not using Open GL

Upvotes: 2

Views: 2634

Answers (1)

DarkDust
DarkDust

Reputation: 92345

Not tested, but it should work like this:

- (void)drawRect:(CGRect)rect
{
    UIColor *color = [UIColor colorWithPatternImage:myPatternImage];
    [color set];

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:p1];
    [path addLineToPoint:p2];
    [path addLineToPoint:p3];
    [path addLineToPoint:p4];
    [path closePath]; // Implicitly does a line between p4 and p1
    [path fill]; // If you want it filled, or...
    [path stroke]; // ...if you want to draw the outline.
}

If you want to stroke it, you might want to set line width via [path setLineWidth:5]; or something like that and also look into the other properties of UIBezierPath that control the line appearance.

Upvotes: 4

Related Questions