【问题标题】:UIImage in a circleUIImage 围成一圈
【发布时间】:2010-12-10 23:34:37
【问题描述】:

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

【问题讨论】:

标签: iphone uiimageview uiimage


【解决方案1】:

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

【讨论】:

  • 一个很好的解决方案!但是,如果您有很多图像
  • cornerRadius 线应该是imageView.layer.cornerRadius = imageView.frame.size.width / 2;
【解决方案2】:

试试这个代码

yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2;
yourImageView.layer.masksToBounds = YES;
yourImageView.layer.borderWidth = 0;

此显示图像类似于 ios 7 圆形图像,谢谢

【讨论】:

  • 这是一个完美的解决方案!
【解决方案3】:

我对用于将 UIImageView 设置为圆形的 swift 扩展的贡献

extension UIImageView{

    func asCircle(){
        self.layer.cornerRadius = self.frame.width / 2;
        self.layer.masksToBounds = true
    }

}

只需拨打MyImageView.asCircle()

【讨论】:

  • 是的,我已经在我的代码中实现了完全相同的解决方案,只是我对 UIView 进行了扩展,因为即使对于其他对象(如按钮... )。
  • 你在哪里存储你的扩展,你创建什么类型的文件,以及根据 MVC 把它放在什么文件夹中?
  • @YashJain,我创建了一个 .swift 文件并将我的所有扩展名存储在那里
【解决方案4】:

使用 UIImageView 并将cornerRadius 设置为高度和宽度的一半。 view.layer.cornerRadius = 角半径;

UIImage rounded corners

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

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

      其他一些选项:

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

      【讨论】:

        【解决方案7】:

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

        1. 将图像的IBOutlet 拖(ctrl + 单击)到您的代码中。

        角半径 为图层背景绘制圆角时使用的半径。动画。 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
        

        【讨论】:

          【解决方案8】:

          我将向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 进行适当的优化调查。
          【解决方案9】:

          在 Objective-C 上它看起来像:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-11-28
            • 1970-01-01
            • 2015-03-07
            • 2019-04-25
            • 1970-01-01
            • 2016-04-07
            • 2019-09-04
            相关资源
            最近更新 更多