xcode UIImageView 边框颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13079612/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:56:30  来源:igfitidea点击:

UIImageView Border Color

iphoneobjective-ciosxcodecgcolor

提问by BlueChips23

I have the following code:

我有以下代码:

UIColor *borderColor = [UIColor colorWithRed:182 green:10 blue:96 alpha:1.0];
[viewImage.layer setBorderColor:borderColor.CGColor];
[viewImage.layer setBorderWidth:3.0];
[productView addSubview:viewImage];

where viewImageis my UIImageViewand productViewis my UIView.

哪里viewImage是我的UIImageViewproductView是我的UIView

I want to get this color enter image description here, but all I am getting is this color enter image description here. I tried different values, and all the colors that I am getting is either white, red, black, yellow, or blue. I can't get any other colors, doesn't matter what values I enter. I tried colorWithHue:Saturation:Brightness and I have the same issue.

我想得到这种颜色在此处输入图片说明,但我得到的只是这种颜色在此处输入图片说明。我尝试了不同的值,我得到的所有颜色都是白色、红色、黑色、黄色或蓝色。我无法获得任何其他颜色,无论我输入什么值。我试过 colorWithHue:Saturation:Brightness 并且我有同样的问题。

I am using this on iOS 6 SDK.

我在 iOS 6 SDK 上使用它。

What am I doing wrong and how can I fix it?

我做错了什么,我该如何解决?

回答by Bajaj

#import <QuartzCore/QuartzCore.h>

[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];

回答by geraldWilliam

The values for red green and blue should be beween 0.0 and 1.0. From UIColor doc:

红色绿色和蓝色的值应介于 0.0 和 1.0 之间。来自 UIColor 文档:

Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.

低于 0.0 的值被解释为 0.0,高于 1.0 的值被解释为 1.0。

回答by Son Nguyen

Try this

尝试这个

UIColor *color = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];

回答by gone

Swift version 2.11 (Xcode 7.2.1):

Swift 2.11 版(Xcode 7.2.1):

let borderColor = UIColor(red:0, green:0, blue:0, alpha:1.0)
viewImage.layer.borderColor = borderColor.CGColor
viewImage.layer.borderWidth = 2.0