I am new to Objective C. I am trying to draw(basic lines as of now) onto a bitmap and then have the bitmap rendered on the screen for Iphone.
Following is the code which i tried, it shows me nothing but a blank screen:
- (void)drawRect:(CGRect)rect
{
CGContextRef screen = UIGraphicsGetCurrentContext();
size_t width = rect.size.width;
size_t height = rect.size.height;
void *data = mallo开发者_如何学编程c(width*height*4);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef c = CGBitmapContextCreate(data, width, height, 8, width*4, colorSpace, kCGImageAlphaPremultipliedFirst);
CGFloat black[4] = {0.0f,0.0f,0.0f,1.0f}; // setting the color in CMYK format
CGContextSetStrokeColor(c, black);
CGContextBeginPath(c);
CGContextMoveToPoint(c, 5.0f, 5.0f);
CGContextAddLineToPoint(c, 50.0f, 50.0f);
CGContextAddLineToPoint(c, 100.0f, 60.0f);
CGContextMoveToPoint(c, 100.0f, 100.0f);
CGContextAddLineToPoint(c, 50.0f, 150.0f);
CGContextStrokePath(c);
CGImageRef img = CGBitmapContextCreateImage(c);
CFRelease(c);
free(data);
CGContextDrawImage(screen, rect, img);
CGContextFlush(screen);
CGContextFlush(c);
CFRelease(colorSpace);
CGImageRelease(img);
CFRelease(screen);
}
Let me know if anyone can point out a similar example or suggest changes to my code.
Thanks for the help everybody... The code works but not as expected.
I modified the code as follows:
CGContextSetRGBFillColor(screen, 1.0, 1.0, 1.0, 1.0); //code works without these 3 lines
CGContextFillRect(screen, rect);
CGContextDrawImage(screen, rect, img);
But there is something weird happening, i can see black lines drawn when the color provided by me is green. Here is the code after creating the bitmap context:
CGFloat black[4] = {0.0f,1.0f,0.0f,1.0f}; //RGB values
CGContextSetStrokeColor(c, black);
This seems to be the only combination for which the code works...
Does anyone have any clue?
First off, you may be aware, but you're doing an extra step here. Although conceptually this approach will work, the drawRect:
gets an implicit graphics context (which you know how to get with UIGraphicsGetCurrentContext
) and you can draw directly into that. There's no need to create the bitmap context and then blit it into the local context afterwards.
That said, your code generally looks reasonable. You're drawing in black without clearing the background to anything else, however, which might explain the all-black result. (The underlying bitmap is just the uninitialized memory from malloc unless you do an explicit clear or fill on it.) Try doing a rect fill on the whole context to white, or etc.
A few other notes:
- You're setting the stroke color in RGBA, not CMYK per your comment, but your result is what you want.
- Don't release the "screen" context-- you don't own that, since you didn't get it via a "create" method.
- I don't think the flushes are required here, but even if they are, you're flushing "c" after you release it-- make sure you do it before if you want to.
- You're drawing only inside the
rect
rectangle. This might be fine in your code, but note that this param can be any sub-rect of your view's bounds. If your intention is to blit the image into the whole view, you should ignore therect
param and just use[self bounds]
as the drawing rectangle.
Is this drawRect code actually getting called? (Did you do a setNeedsDisplay somewhere else?)
What values is drawRect getting for width and height? Does your drawing fit inside those coordinates?
Also, you may be drawing a black line to an initially black (0,0,0) bitmap. Try a different color (red, for instance).
You can also dump the bytes of your bitmap data to a file and see how they are initialized, and if they are getting changed by your drawing commands.
ADDED:
Try using the iOS API for setting your colors:
CGContextSetRGBFillColor(c, 1.0, 0.0, 0.0, 1.0); // RGBA
精彩评论