开发者

How to get each pixel position (Coordinate) of a Hollow Character drawn using Core Graphics?

I am using UIView subclass to draw an alphabet using Core Graphics.Using this code only hollow characters are drwan(By Making the stroke color- black color).Here I need each pixel(Coordinate) position of the character.Please give me an idea how I will get each pixel point(Only the black point's pixel value) of the hollow character.

Source Code:

/*UIView Subclass Method*/

- (void)drawRect:(CGRect)rect
{
      //Method to draw the alphabet
      [self drawChar:@"A" xcoord:5 ycoords:35];
}
-(void)drawChar:(NSString *)str xcoord: (CGFloat)x ycoord: (CGFloat)y
{

    const char *text = [str UTF8String];//Getting the alphabet
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSelectFont(ctx, "Helvetica", 40.0, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(ctx, kCGTextFillStro开发者_C百科ke);
    CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);//to make the character hollow-Need only these points
    CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);//Fill color of character is clear color

    CGAffineTransform xform = CGAffineTransformMake(
                                                    1.0,  0.0,
                                                    0.0, -1.0,
                                                    0.0,  0.0);//Giving a transformation to the alphabet

    CGContextSetTextMatrix(ctx, xform);

   CGContextShowTextAtPoint(ctx, x, y, text, strlen(text));//Generating the character
}


Firstly, there's convenient method to set the fill and stroke color:

[[UIColor blackColor] setStroke];
[[UIColor clearColor] setFill];

either that or use CGContextSetGrayFillColor and CGContextSetGrayStrokeColor.


As for your problem, to get pixel information, you have to use a CGBitmapContext. You can get the bitmap by CGBitmapContextGetData. Suppose you declare an RGBA bitmap, the bitmap data will be arranged as

RGBA_at_(0,0)  RGBA_at_(1,0)  ...  RGBA_at_(w,0)  RGBA_at(0,1)  RGBA_at_(1,1) ...

therefore, you can scan linearly for any target color values and determine the black pixels.


Please note that UIGraphicsGetCurrentContext() is not necessarily a CGBitmapContext, so you should not apply CGBitmapContextGetData on it. Instead, you have to create your own bitmap context with CGBitmapContextCreate, draw the text onto this bitmap context, obtain the CGImage result by CGBitmapContextCreateImage, and finally draw this CGImage onto the UIContext.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜