【发布时间】:2010-12-10 23:34:37
【问题描述】:
有人可以帮我吗?新作为 iPhone 开发人员。我正在尝试以圆形而不是 iPhone 标准的矩形显示 .png 图片
【问题讨论】:
标签: iphone uiimageview uiimage
有人可以帮我吗?新作为 iPhone 开发人员。我正在尝试以圆形而不是 iPhone 标准的矩形显示 .png 图片
【问题讨论】:
标签: iphone uiimageview uiimage
好吧,所有 png 文件都是“矩形”,但如果您想在屏幕上显示圆形或其他非矩形对象,您可以使用透明度来实现。为了确保图像中的透明像素在 iPhone 上也是透明的,您可以将 UIImageView 的背景颜色设置为清除。这可以在 Interface Builder 中通过将背景颜色选择器中的不透明度滑块一直向下拖动来完成, 或代码如下:
UIImage *image = [UIImage imageNamed:@"yourRoundImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.backgroundColor = [UIColor clearColor];
[self.view addSubview: imageView];
如果你只是想添加圆角,做一个圆,如果你已经将 QuartzCore 框架添加到你的项目中,你也可以像这样使用cornerRadius 属性:
#import <QuartzCore/QuartzCore.h>
UIImage *image = [UIImage imageNamed:@"yourRoundImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.layer.cornerRadius = image.size.width / 2;
imageView.layer.masksToBounds = YES;
[self.view addSubview: imageView];
【讨论】:
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
试试这个代码
yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2;
yourImageView.layer.masksToBounds = YES;
yourImageView.layer.borderWidth = 0;
此显示图像类似于 ios 7 圆形图像,谢谢
【讨论】:
我对用于将 UIImageView 设置为圆形的 swift 扩展的贡献
extension UIImageView{
func asCircle(){
self.layer.cornerRadius = self.frame.width / 2;
self.layer.masksToBounds = true
}
}
只需拨打MyImageView.asCircle()
【讨论】:
使用 UIImageView 并将cornerRadius 设置为高度和宽度的一半。 view.layer.cornerRadius = 角半径;
【讨论】:
试试这个来获得图像视图的圆角并为角落着色:
self.imgView.layer.cornerRadius =self.imgView.frame.size.height/2;
self.imgView.layer.masksToBounds = YES;
self.imgView.layer.borderColor = [UIColor colorWithRed:148/255. green:79/255. blue:216/255. alpha:1.0].CGColor;
self.imgView.layer.borderWidth=2;
条件*:imageView的高度和宽度必须相同才能得到圆角。
【讨论】:
如果您的视图中只有几个图像,则更改图像视图的角半径效果很好。但是,如果图像视图在 tableview 中,性能会受到影响。
其他一些选项:
【讨论】:
Swift 4:这应该会在一个圆圈中显示您的 .png。
IBOutlet 拖(ctrl + 单击)到您的代码中。角半径 为图层背景绘制圆角时使用的半径。动画。 https://developer.apple.com/documentation/quartzcore/calayer/1410818-cornerradius
clipsToBounds 属性 一个布尔值,用于确定子视图是否限制在视图的边界内。 https://developer.apple.com/documentation/uikit/uiview/1622415-clipstobounds
2.在viewDidLoad()中,使用实例属性layer.cornerRadius和clipsToBounds。
profileImage.layer.cornerRadius = 50
profileImage.clipsToBounds = true
【讨论】:
我将向UIImageView 添加一个更通用的扩展,该扩展将适用于非方形图像。
需要注意的是,它的运行速度会比cornerRadius 方法慢。
extension UIImageView {
@IBInspectable public var asEllipse:Bool {
get {
if let mask = self.layer.mask {
return mask.name == kMaskLayerName
}
return false;
}
set {
if (newValue) {
let ellipseMask = CAShapeLayer()
ellipseMask.name = kMaskLayerName
ellipseMask.path = CGPathCreateWithEllipseInRect(self.bounds, nil)
ellipseMask.strokeColor = UIColor.clearColor().CGColor
ellipseMask.fillColor = UIColor.whiteColor().CGColor
self.layer.mask = ellipseMask
} else if self.asEllipse {
self.layer.mask = nil
}
}
}
}
private let kMaskLayerName="EllipseMaskLayer"
【讨论】:
cornerRadius慢?
CAShapeLayer、一个CGPath和两个UIColor实例。尽管在 iPhone 5 及更高版本上测试了tableView 中的单元格图像,但滚动时没有明显的减速。 TBH 我还没有对 Instruments 进行适当的优化调查。
在 Objective-C 上它看起来像:
UIImage* pIconImage = [UIImage imageNamed:@"ImageName"];
UIImageView* pIconView = [[UIImageView alloc] initWithImage:pIconImage];
[pIconView.layer setCornerRadius:pIconImage.size.width / 2];
[pIconView.layer setMasksToBounds:YES];
【讨论】: