xcode 为iphone中的图像添加边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1582002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add border to a image in iphone
提问by Iya
I have an image in a custom cell. Is there any api to add a gray border to an image?
我在自定义单元格中有一个图像。是否有任何api可以为图像添加灰色边框?
Thanks in advance!
提前致谢!
回答by Brad Larson
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:
如果您使用的是 iPhone OS 3.0,您可以使用图像视图的 CALayer 的 borderWidth 和 borderColor 属性在图像上添加边框,如下所示:
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.
这将创建一个 4 像素宽的边框,具有完全不透明的颜色,其 RGB 值介于白色和黑色之间。
回答by Deepak
you can try this
你可以试试这个
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
for this you need import <QuartzCore/QuartzCore.h>
为此,您需要导入 <QuartzCore/QuartzCore.h>
回答by luvieere
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.
我不这么认为,但你可以放一个灰色的 UIView 而不是你的图像,然后将图像添加为这个灰色 UIView 的子项。通过适当地设置其框架,将图像缩小一点,并将其放在中心,这样您就只能看到后面 UIView 的几个像素。这样,它看起来就好像图像有边框一样。