Drawing in the context of a bitmap

I am trying to draw in a raster context, but it is empty. I believe that I am creating things correctly, because I can initialize the context, draw several things, and then create an image from it and draw this image. What I can not do, after initialization, is once again draw a context that draws more elements on it. I'm not sure that I missed any ordinary practice, which implies that I can only draw it in certain places or that I have to do something else. Here is what I do below.

I copied the helper function provided by the apple, with one modification, to get the color space, because it did not compile (this is for the iPad, I don't know if that matters):

CGContextRef MyCreateBitmapContext (int pixelsWide, int pixelsHigh)
{
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow   = (pixelsWide * 4);// 1
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);


    colorSpace = CGColorSpaceCreateDeviceRGB();  //CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);// 2

    bitmapData = malloc( bitmapByteCount );// 3
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        return NULL;
    }
    context = CGBitmapContextCreate (bitmapData,// 4
                                    pixelsWide,
                                    pixelsHigh,
                                    8,      // bits per component
                                    bitmapBytesPerRow,
                                    colorSpace,
                                    kCGImageAlphaPremultipliedLast);
    if (context== NULL)
    {
        free (bitmapData);// 5
        fprintf (stderr, "Context not created!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );// 6

    return context;// 7
}

init , , :

    mContext = MyCreateBitmapContext (rect.size.width, rect.size.height);

    // sample fills
    CGContextSetRGBFillColor (mContext, 1, 0, 0, 1);
    CGContextFillRect (mContext, CGRectMake (0, 0, 200, 100 ));
    CGContextSetRGBFillColor (mContext, 0, 0, 1, .5);
    CGContextFillRect (mContext, CGRectMake (0, 0, 100, 200 ));


    CGContextSetRGBStrokeColor(mContext, 1.0, 1.0, 1.0, 1.0);
    CGContextSetRGBFillColor(mContext, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(mContext, 5.0);
    CGContextAddEllipseInRect(mContext, CGRectMake(0, 0, 60.0, 60.0));
    CGContextStrokePath(mContext);

drawRect , . , var , - , ? ( ):

// draw bitmap context
CGImageRef myImage = CGBitmapContextCreateImage (mContext);
CGContextDrawImage(context, rect, myImage);
CGImageRelease(myImage);

, , , , , :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint location;
    for (UITouch* touch in touches)
    {
        location = [touch locationInView: [touch view]];
    }
    CGContextSetRGBStrokeColor(mContext, 1.0, 1.0, 1.0, 1.0);
    CGContextSetRGBFillColor(mContext, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(mContext, 2.0);
    CGContextAddEllipseInRect(mContext, CGRectMake(location.x, location.y, 60.0, 60.0));
    CGContextStrokePath(mContext);
}

+3
1

[self setNeedsDisplay];!!!!

!!!!!!!!!

, drawRect init, , . , setNeedsDisplay , , , , .:)

+1

Source: https://habr.com/ru/post/1769831/


All Articles