tags:

views:

476

answers:

2

I have an image in a custom cell. Is there any api to add a gray border to an image?

Thanks in advance!

+1  A: 

I don't think so, but you can put a gray UIView instead of your image, and then add the image as child of this gray UIView. Make the image a little smaller, and put it in the center, by setting its frame appropriately, so that you'll only see a few pixels from the UIView behind. This way, it will appear as if the image has a border.

luvieere
+3  A: 

If you are on iPhone OS 3.0, you can use the borderWidth and borderColor properties of your image view's CALayer to add a border over the image, as follows:

imageView.layer.borderWidth = 4.0f;
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat values[4] = {0.5, 0.5, 0.5, 1.0}; 
CGColorRef grey = CGColorCreate(space, values); 
imageView.layer.borderColor = grey;
CGColorRelease(grey);
CGColorSpaceRelease(space);

This creates a 4-pixel-wide border with a fully opaque color having RGB values midway between white and black.

Brad Larson
Why do you not just use UIColor *color = [UIColor colorWithRed:0.5f Green:0.5f Blue:0.5f alpha:1.0f]; imageView.layer.borderColor = color.CGColor ??
Kendall Helmstetter Gelner
I'd pulled that color code from some Mac / iPhone cross-platform stuff that I'd written. Aside from working on both platforms, there might be a slight performance and memory advantage in avoiding creating a temporary UIColor instance just to extract the CGColorRef. That would only come into play with frequent drawing, at which point I'd recommend caching the colorspace and color anyway.
Brad Larson