views:

193

answers:

2

Hi folks,

I am wondering which is the best way, in terms of speed and efficiency, to draw a frame around an image on iPhone, especially when I have to draw lots of these images:

1) Drawing the image and then the frame around or 2) Drawing a rect, filling it with a color and then drawing the image within that rect leaving some offset pixel to mimic the frame

Does Quartz draw everything that it is told to or is it smart enough to draw only what is really visible? My feeling is that the first approach is better because there is actually less drawing done. Is it really so?

Thanks P.

A: 

If performance is a problem, you should try to minimize the number of operations you perform on the graphics context, especially the ones that have no visible components.

In your particular case, I think you need to test both options on an iPhone (ist gen if possible) and benchmark them. Maybe it's easier to just fill the whole rectangle rather than calculate which pixels are part of the frame and which aren't?

It depends on the graphics chip.

Rob Fonseca-Ensor
They are just rectangular images so drawing a border is as easy as drawing a rect and stroke it. Anyway, I guess I have to do some real benchmarks.
Paolo.nl
+1  A: 

Quartz drawing will only take place within the bounds of the view, if you are doing custom drawing in -drawRect:.

That said, I think that you will see the best performance if you simply create UIImageViews for each image, then use the borderWidth, borderColor, and possibly cornerRadius properties on your view's layer to set a border. For example:

imageView.layer.cornerRadius = 10.0f;
imageView.layer.borderWidth = 3.0f;
imageView.layer.borderColor = [[UIColor blackColor] CGColor];

will place a 3-pixel-wide black border around your view, and give it a 10 pixel radius at the corners.

Brad Larson
I thought about this solution but my images are drawn in a custom UITableViewCell and for achieving smooth scrolling I am drawing everything by myself in drawRect instead of adding subviews. I am wondering if it is possible to draw a subview in the drawRect method of its superview? Is there a method, like NSString's drawAtPoint or drawInRect, for UIViews?
Paolo.nl
You can use -renderInContext: for the UIView's layer, and that will draw the content of most layers directly into a Quartz context.
Brad Larson