繁体   English   中英

圆形中的 UIImage

[英]UIImage in a circle

有人可以在这里帮助我吗? 新作为 iPhone 开发人员。 我试图在圆形而不是矩形中显示 .png 图片,这是 iPhone 的标准

好吧,所有的 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];

试试这个代码

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 = 角半径;

UIImage 圆角

试试这个以获得图像视图的圆角并为角着色:

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 的高度和宽度必须相同才能获得圆角。

如果您的视图中只有几个图像,则更改图像视图的cornerRadius 效果很好。 但是,如果图像视图在 tableview 中,性能将受到影响。

其他一些选项:

  1. 将图像资源在服务器上制作成圆形,如果它们被捆绑到应用程序中,则手动制作,圆形的外部区域使用透明部分。
  2. 如果图像视图的背景没有改变,则创建一个覆盖图像,其内圈部分透明,其余部分与背景相同。 还将图像视图的 backgroundColor 设置为 clearColor。
  3. 接收图像时,在代码中将它们编辑为一个圆圈,在后台线程中。

Swift 4:这应该会在一个圆圈中显示您的 .png。

  1. 将图像的IBOutlet拖向您的代码(按住 ctrl + 单击)。

在此处输入图片说明

cornerRadius 为图层背景绘制圆角时使用的半径。 可动画。 https://developer.apple.com/documentation/quartzcore/calayer/1410818-cornerradius

clipsToBounds 属性 一个布尔值,用于确定子视图是否限制在视图的边界内。 https://developer.apple.com/documentation/uikit/uiview/1622415-clipstobounds

2.在viewDidLoad()里面,使用实例属性layer.cornerRadiusclipsToBounds

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"

在 Objective-C 上,它看起来像:

UIImage* pIconImage = [UIImage imageNamed:@"ImageName"];
UIImageView* pIconView = [[UIImageView alloc] initWithImage:pIconImage];
[pIconView.layer setCornerRadius:pIconImage.size.width / 2];
[pIconView.layer setMasksToBounds:YES];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM