Note that I have modified your code a bit to test it. But this takes array coordinates and draws a shape based on line coordinates. The uses that shape to draw pattern over 1000x1000 image. Final image is saved in your photo album so that you can test out the code.. You can replace it with return of UIImage as per your original code.. However this primarily shows you the technique how you can use the drawing to create texture.
- (void)drawTexture : (NSArray *)verticesPassed {
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
//This function gets the bounds or smallest rectangle required to generate a shape which
//will be used as pattern
CGRect shp = [self boundFromFrame:verticesPassed];
//Generate the shape as image so that we can make pattern out of it.
CGContextRef conPattern = CGBitmapContextCreate(NULL,
shp.size.width,
shp.size.height,
8,
0,
rgbColorSpace,
kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(rgbColorSpace);
CGContextSetLineWidth(conPattern, 10);
CGContextSetStrokeColorWithColor(conPattern, [UIColor blueColor].CGColor);
Line * start = [verticesPassed objectAtIndex:0];
CGContextMoveToPoint(conPattern, start.x-shp.origin.x , start.y-shp.origin.y);
for (Line * vertice in verticesPassed) {
CGContextAddLineToPoint(conPattern, vertice.x-shp.origin.x , vertice.y-shp.origin.y );
}
CGContextStrokePath(conPattern);
//Make the main image and color it with pattern.
CGImageRef cgImage = CGBitmapContextCreateImage(conPattern);
UIImage *imgPattern = [[UIImage alloc]initWithCGImage:cgImage];
//UIImageWriteToSavedPhotosAlbum(imgPattern, nil, nil, nil);
UIColor *patternColor = [UIColor colorWithPatternImage:imgPattern];
CGContextRef con = CGBitmapContextCreate(NULL,
1000,
1000,
8,
0,
rgbColorSpace,
kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(rgbColorSpace);
CGContextSetLineWidth(con, 10);
CGContextMoveToPoint(con, 0 , 0);
CGContextAddLineToPoint(con, 1000 , 0 );
CGContextAddLineToPoint(con, 1000 , 1000 );
CGContextAddLineToPoint(con, 0 , 10000 );
CGContextSetFillColorWithColor(con, patternColor.CGColor);
CGContextFillPath(con);
CGImageRef cgImageFinal = CGBitmapContextCreateImage(con);
UIImage *newImage = [[UIImage alloc]initWithCGImage:cgImageFinal];
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
}
-(CGRect)boundFromFrame:(NSArray*)verticesPassed
{
float top,left,right,bottom;
bool bFirst = YES;
for (Line * vertice in verticesPassed) {
if(bFirst)
{
left = right = vertice.x;
top = bottom = vertice.y;
bFirst = NO;
}
else{
if (vertice.x<left) left = vertice.x;
if (vertice.x>right) right = vertice.x;
if (vertice.x<top) top = vertice.y;
if (vertice.x>bottom) bottom = vertice.y;
}
}
return CGRectMake(left, top, right - left, bottom-top);
}