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

3

我正在使用以下代码绘制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);

}

在相册中:

在此输入图片描述


你能发布最终想要的图像吗?如果相同的模式需要覆盖整个1000x1000的图像,为什么还需要传递顶点数组呢? - TorukMakto
这是一个处理各种形状的类 - 例如正方形、三角形、不规则形状。我可以绘制这些形状,现在正在尝试创建相匹配的纹理。 - GuybrushThreepwood
1
明白了 - 我能够帮忙 - 我过去做过类似的事情。 - TorukMakto
嗨,你能给出“Line”和“StandPoint”的定义吗? - TorukMakto
两者都是核心数据对象。每行包含一个起点和终点的 standPoint,其中包含 x 和 y 点。 - GuybrushThreepwood
显示剩余2条评论
1个回答

1
请注意,我稍微修改了您的代码以进行测试。但是,它使用数组坐标并根据线条坐标绘制形状。然后使用该形状在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);

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接