使用CGBitmapContextCreate绘制UIImage-纹理的全尺寸图像-iOS

使用CGBitmapContextCreate绘制UIImage-纹理的全尺寸图像-iOS,ios,objective-c,uiimage,quartz-2d,cgbitmapcontextcreate,Ios,Objective C,Uiimage,Quartz 2d,Cgbitmapcontextcreate,我正在使用下面的代码来绘制UIImage。在本例中,我使用一些顶点绘制正方形: - (UIImage *)drawTexture : (NSArray *)verticesPassed { CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef con = CGBitmapContextCreate(NULL,

我正在使用下面的代码来绘制UIImage。在本例中,我使用一些顶点绘制正方形:

- (UIImage *)drawTexture : (NSArray *)verticesPassed {

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef con = CGBitmapContextCreate(NULL,
                                                               1000,
                                                               1000,
                                                               8,
                                                               0,
                                                               rgbColorSpace,
                                                               kCGImageAlphaPremultipliedFirst);

    CGColorSpaceRelease(rgbColorSpace);

    CGContextSetLineWidth(con, 10);

    Line * start = [verticesPassed objectAtIndex:0];
    StandPoint * startPoint = start.origin;

    CGContextMoveToPoint(con, [startPoint.x floatValue], [startPoint.y floatValue]);

    for (Line * vertice in verticesPassed) {
        StandPoint * origin = vertice.origin;
        CGContextAddLineToPoint(con, [origin.x floatValue], [origin.y floatValue]);
        NSLog(@"Texutre point is %f %f", [origin.x floatValue], [origin.y floatValue]);
    }

    CGContextSetFillColorWithColor(con, [UIColor greenColor].CGColor);

    CGContextFillPath(con);

    [self drawText:con startX:250 startY:200 withText:standName];
    [self drawText:con startX:250 startY:150 withText:standNumber];


    CGImageRef cgImage = CGBitmapContextCreateImage(con);

    UIImage *newImage = [[UIImage alloc]initWithCGImage:cgImage];

    NSLog(@"Size is %f", newImage.size.height);

    return newImage;

}
我的正方形的顶点是:

Texutre point is 667.000000 379.000000
Texutre point is 731.000000 379.000000
Texutre point is 731.000000 424.000000
Texutre point is 667.000000 424.000000
问题是,在1000x1000上下文中,这显然在上下文的右上角绘制了一个非常小的形状

由于我想将此UIImage用作纹理,我的问题是如何在没有任何空白的情况下创建正确大小的形状(即从0,0开始)

来自Bugivore的代码:

- (UIImage *)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,
                                                    (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);

    CGColorSpaceRelease(rgbColorSpace);

    CGContextSetLineWidth(conPattern, 10);
    CGContextSetStrokeColorWithColor(conPattern, [UIColor blueColor].CGColor);

    Line * start = [verticesPassed objectAtIndex:0];
    StandPoint * startPoint = start.origin;
    CGContextMoveToPoint(conPattern, [startPoint.x floatValue]-shp.origin.x , [startPoint.y floatValue]-shp.origin.y);

    for (Line * vertice in verticesPassed) {
        StandPoint * standPoint = vertice.origin;
        CGContextAddLineToPoint(conPattern, [standPoint.x floatValue]-shp.origin.x, [standPoint.y floatValue]-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,
                                             500,
                                             500,
                                             8,
                                             0,
                                             rgbColorSpace,
                                             (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);

    CGColorSpaceRelease(rgbColorSpace);

    CGContextSetLineWidth(con, 10);

    CGContextMoveToPoint(con, 0 , 0);
    CGContextAddLineToPoint(con, 500 , 0 );
    CGContextAddLineToPoint(con, 500, 500 );
    CGContextAddLineToPoint(con, 0 , 500);


    CGContextSetFillColorWithColor(con, patternColor.CGColor);

    CGContextFillPath(con);


    CGImageRef cgImageFinal = CGBitmapContextCreateImage(con);

    UIImage *newImage = [[UIImage alloc]initWithCGImage:cgImageFinal];

    UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);

    return newImage;


}
-(CGRect)boundFromFrame:(NSArray*)verticesPassed
{
    float top,left,right,bottom;
    bool bFirst = YES;

    for (Line * vertice in verticesPassed) {
        StandPoint * standPoint = vertice.origin;
        if(bFirst)
        {
            left = right = [standPoint.x floatValue];
            top = bottom = [standPoint.y floatValue];
            bFirst = NO;
        }
        else{
            if ([standPoint.x floatValue]<left) left = [standPoint.x floatValue];
            if ([standPoint.x floatValue]>right) right = [standPoint.x floatValue];
            if ([standPoint.x floatValue]<top) top = [standPoint.y floatValue];
            if ([standPoint.x floatValue]>bottom) bottom = [standPoint.y floatValue];
        }

    }

    return CGRectMake(left, top, right - left, bottom-top);

}
-(UIImage*)drawTexture:(NSArray*)垂直传递{
CGColorSpaceRef rgbColorSpace=CGColorSpaceCreateDeviceRGB();
//此函数获取生成形状所需的边界或最小矩形,该形状
//将用作图案
CGRect shp=[self-boundFromFrame:verticesPassed];
//将形状生成为图像,以便我们可以将其制成图案。
CGContextRef conPattern=CGBitmapContextCreate(空,
shp.size.width,
shp.尺寸.高度,
8.
0,
RGB颜色空间,
(CGBitmapInfo)KCGIMAGEAlphaPremultipledFirst);
CGBColorSpace;
CGContextSetLineWidth(conPattern,10);
CGContextSetStrokeColorWithColor(conPattern,[UIColor blueColor].CGColor);
行*开始=[verticesPassed objectAtIndex:0];
视点*startPoint=start.origin;
CGContextMoveToPoint(conPattern,[startPoint.x floatValue]-shp.origin.x,[startPoint.y floatValue]-shp.origin.y);
用于(垂直中的线*垂直通过){
立足点*立足点=垂直原点;
CGContextAddLineToPoint(conPattern,[Pointpoints.x floatValue]-shp.origin.x,[Pointpoints.y floatValue]-shp.origin.y);
}
CGContextStrokePath(conPattern);
//制作主图像并用图案着色。
CGImageRef cgImage=CGBitmapContextCreateImage(conPattern);
UIImage*imgPattern=[[UIImage alloc]initWithCGImage:cgImage];
//UIImageWriteToSavedPhotosAlbum(imgPattern,nil,nil,nil);
UIColor*patternColor=[uicolorWithPatternImage:imgPattern];
CGContextRef con=CGBitmapContextCreate(空,
500,
500,
8.
0,
RGB颜色空间,
(CGBitmapInfo)KCGIMAGEAlphaPremultipledFirst);
CGBColorSpace;
CGContextSetLineWidth(con,10);
CGContextMoveToPoint(con,0,0);
CGContextAddLineToPoint(con,500,0);
CGContextAddLineToPoint(con,500500);
CGContextAddLineToPoint(con,0500);
CGContextSetFillColorWithColor(con,patternColor.CGColor);
CGContextFillPath(con);
CGImageRef cgImageFinal=CGBitmapContextCreateImage(con);
UIImage*newImage=[[UIImage alloc]initWithCGImage:cgImageFinal];
UIImageWriteToSavedPhotosAlbum(newImage,nil,nil,nil);
返回新图像;
}
-(CGRect)boundFromFrame:(NSArray*)垂直传递
{
浮动顶部、左侧、右侧、底部;
bool bFirst=是;
用于(垂直中的线*垂直通过){
立足点*立足点=垂直原点;
如果(bFirst)
{
左=右=[立场.x浮点值];
顶部=底部=[立场.y浮动值];
bFirst=否;
}
否则{
如果([standPoint.x floatValue]right)right=[standPoint.x floatValue];
如果([standPoint.x floatValue]bottom)bottom=[standPoint.y floatValue];
}
}
返回CGRectMake(左、上、右-左、下-上);
}
相册中:


请注意,我对您的代码进行了一些修改,以对其进行测试。但这需要阵列坐标并基于线坐标绘制形状。使用该形状在1000x1000图像上绘制图案。最终图像保存在相册中,以便您可以测试代码。。您可以根据原始代码将其替换为返回UIImage。。但是,这主要向您展示了如何使用图形创建纹理的技术

- (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);

}
-(void)drawTexture:(NSArray*)垂直传递{
CGColorSpaceRef rgbColorSpace=CGColorSpaceCreateDeviceRGB();
//此函数获取生成形状所需的边界或最小矩形,该形状
//将用作图案
CGRect shp=[self-boundFromFrame:verticesPassed];
//将形状生成为图像,以便我们可以将其制成图案。
CGContextRef conPattern=CGBitmapContextCreate(空,
shp.size.width,
shp.尺寸.高度,
8.
0,
RGB颜色空间,
KCGIMAGEAlphaPremultipledFirst);
CGBColorSpace;
CGContextSetLineWidth(conPattern,10);
CGContextSetStrokeColorWithColor(conPattern,[UIColor blueColor].CGColor);
行*开始=[verticesPassed objectAtIndex:0];
CGContextMoveToPoint(conPattern,start.x-shp.origin.x,start.y-shp.origin.y);
用于(垂直中的线*垂直通过){
CGContextAddLineToPoint(conPattern,vertice.x-shp.origin.x,vertice.y-shp.origin.y);
}
CGContextStrokePath(conPattern);
//制作主图像并用图案着色。
CGImageRef cgImage=CGBitmapContextCreateImage(conPattern);
UIImage*imgPattern=[[UIImage alloc]initWithCGImage:cgImage];
//UIImageWriteToSavedPhotosAlbum(imgPattern,nil,nil,nil);
UIColor*patternColor=[uicolorWithPatternImage:imgPattern];
CGContextRef con=CGBitmapContextCreate(空,