Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
Hey all. I'm working on trying to do some custom 2D drawing on the iPhone, and I'm running into some issues. I don't have any Quartz/OpenGL experience, but I know a good bit of Cocoa.

I have a subclass of an UISimpleTableViewCell, and I'm trying to draw a simple triangle in it. I had been drawing squares by creating really small UIViews and just setting their background color, but that's obviously the wrong way to do this. :)

So, here's what I'm trying so far:

Code:
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetRGBFillColor(context, 0.5, 1.0, 0.5, 0.8); 
CGRect rect = CGRectMake(0, 0, 200, 200);
CGContextFillRect(context, rect);

And it's not actually drawing anything in the view. Again, I'm a complete beginner in this topic, but I think once I get this figured out, I'd like to move to using the CGContextAddLineToPoint, CGContextAddArc, etc. methods, and I was wondering if you guys could help me out with some *very* simple sample code. Eg., maybe just drawing a rectangle and filling it.

I've looked at a lot of Apple's sample code related to this topic, but it was a bit over my head. Thanks! :D

Edit: Just for some additional info, the above code is in my Cell's initWithFrame: method, after any subviews are added.
 

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
Shouldn't you be drawing in drawRect:?
Ah, good point.

I created a subclass of UIView, called TriangleView, and moved those 4 lines of code into the drawRect: method.

Then in my UITableViewCell subclass, I instantiate one of those TriangleViews and add it as a subview to the cell, and it works fine! Thanks for the hint! :)

Now I just need to work on drawing lines and other cool shapes. :D
 

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
Hmm, I seem to have fun into another problem. Is there any way to either:

1) Convert a UIColor to a CGColorRef?

2) Obtain the RGB values from a UIColor?

Using the CGPaths you can only draw using a CGColorRef color, but I have a color stored as a UIColor that I need to set. Is there any way to do this? Thanks! :D
 

lucasgladding

macrumors 6502
Feb 16, 2007
319
1
Waterloo, Ontario
Hmm, I seem to have fun into another problem. Is there any way to either:

1) Convert a UIColor to a CGColorRef?

2) Obtain the RGB values from a UIColor?

Using the CGPaths you can only draw using a CGColorRef color, but I have a color stored as a UIColor that I need to set. Is there any way to do this? Thanks! :D

1 - I missed the single property in the UIColor documentation initially too ;)
2 - Check the documentation for CGColorRef and look for CGColorGetComponents and CGColorGetColorSpace. The components in an RGB space should give you what you need.

Good luck with everything. Also, I would highly recommend the Quartz 2D Programming Guide if the CG methods are new to you. Nearly everything here overlaps with Mac development, so you shouldn't have any trouble finding info (as this isn't under NDA).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.