如何在swift中设置圆形图像

问题描述 投票:65回答:14

如何用快速制作圆形图片?

我的ViewController:

import UIKit
import Foundation

class FriendsViewController : UIViewController{

    @IBOutlet weak var profilPicture: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        profilPicture = UIImageView(frame: CGRectMake(0, 0, 100, 100))
    }
}

我的profilPicture = UIImageView(frame: CGRectMake(0, 0, 100, 100))什么都不做..

示例:http://www.appcoda.com/ios-programming-circular-image-calayer/

ios swift uiimageview
14个回答
175
投票
import UIKit

class ViewController: UIViewController {
  @IBOutlet weak var image: UIImageView!

  override func viewDidLoad() {
    super.viewDidLoad()

    image.layer.borderWidth = 1
    image.layer.masksToBounds = false
    image.layer.borderColor = UIColor.black.cgColor
    image.layer.cornerRadius = image.frame.height/2
    image.clipsToBounds = true
}

这就是你所需要的......


0
投票

如果您的图像是圆形的,它的高度和宽度将完全相同(即120)。您只需使用该数字的一半并在代码中使用它(image.layer.cornerRadius = 60)。


0
投票

Download and use Toucan from GitHub.它是在Swift中提供的。心里难以理解。它还提供了许多其他图像效果。


0
投票

这种方式是最便宜的方式,并始终保持您的图像视图圆形:

class RoundedImageView: UIImageView {

    override init(frame: CGRect) {
        super.init(frame: frame)

        clipsToBounds = true
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        clipsToBounds = true
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        assert(bounds.height == bounds.width, "The aspect ratio isn't 1/1. You can never round this image view!")

        layer.cornerRadius = bounds.height / 2
    }
}

其他答案告诉您根据UIViewControllers viewDidLoad()方法中设置的帧计算来舍入视图。这是不正确的,因为它不确定最终的帧是什么。


0
投票
// code to make the image round


import UIKit

extension UIImageView {
public func maskCircle(anyImage: UIImage) {


    self.contentMode = UIViewContentMode.ScaleAspectFill
    self.layer.cornerRadius = self.frame.height / 2
    self.layer.masksToBounds = false
    self.clipsToBounds = true




    // make square(* must to make circle),
    // resize(reduce the kilobyte) and
    // fix rotation.
    //        self.image = prepareImage(anyImage)
}
}

//从视图控制器调用该函数

self.imgCircleSmallImage.maskCircle(imgCircleSmallImage.image!)

0
投票
struct CircleImage: View {
    var image: Image

    var body: some View {
        image
           .clipShape(Circle())
    }
}

这对SwiftUI来说是正确的


35
投票

你可以简单地创建扩展:

import UIKit

extension UIImageView {

   func setRounded() {
      let radius = CGRectGetWidth(self.frame) / 2
      self.layer.cornerRadius = radius
      self.layer.masksToBounds = true
   }
}

并使用如下:

imageView.setRounded()

14
投票

基于@DanielQ的答案

Swift 4和Swift 3

import UIKit

extension UIImageView {

    func setRounded() {
        self.layer.cornerRadius = (self.frame.width / 2) //instead of let radius = CGRectGetWidth(self.frame) / 2
        self.layer.masksToBounds = true
    }
}

您可以在任何ViewController中使用它:

imageView.setRounded()

11
投票

如果你的意思是你想在Swift中创建一个UIImageView循环,你可以使用这个代码:

imageView.layer.cornerRadius = imageView.frame.height / 2
imageView.clipsToBounds = true

6
投票

不知道这是否对任何人都有帮助,但我一直在努力解决这个问题,网上的答案都没有帮助我。对我来说问题是我在故事板中的图像上设置了不同的高度和宽度。我尝试了堆栈上的每个解决方案,事实证明它就是那么简单。一旦我将它们设置为200,我的圆形轮廓图像就完美了。这是我的VC中的代码。

profileImage2.layer.cornerRadius = profileImage2.frame.size.width/2
    profileImage2.clipsToBounds = true

4
投票

对于Swift 4:

import UIKit

extension UIImageView {

func makeRounded() {
    let radius = self.frame.width/2.0
    self.layer.cornerRadius = radius
    self.layer.masksToBounds = true
   }
}

2
投票

上面的所有答案都无法解决我的问题。我的ImageView被放置在一个定制的UITableViewCell。因此我也从这里调用了layoutIfNeeded()方法。例:

 class NameTableViewCell:UITableViewCell,UITextFieldDelegate { ...

 override func awakeFromNib() {

    self.layoutIfNeeded()
    profileImageView.layoutIfNeeded()

    profileImageView.isUserInteractionEnabled = true
    let square = profileImageView.frame.size.width < profileImageView.frame.height ? CGSize(width: profileImageView.frame.size.width, height: profileImageView.frame.size.width) : CGSize(width: profileImageView.frame.size.height, height:  profileImageView.frame.size.height)
    profileImageView.addGestureRecognizer(tapGesture)
    profileImageView.layer.cornerRadius = square.width/2
    profileImageView.clipsToBounds = true;
}

1
投票

首先需要设置相等的宽度和高度以获得圆形ImageView.Below我将宽度和高度设置为100,100。如果要根据所需的大小设置相等的宽度和高度,请在此处设置。

 var imageCircle = UIImageView(frame: CGRectMake(0, 0, 100, 100))

然后你需要为角半径设置高度/ 2

 imageCircle.layer.cornerRadius = imageCircle.frame.size.height/2
 imageCircle.layer.borderWidth = 1
 imageCircle.layer.borderColor = UIColor.blueColor().CGColor
 imageCircle.clipsToBounds = true
 self.view.addSubview(imageCircle)

1
投票

对于Swift3 / Swift4开发人员:

let radius = yourImageView.frame.width / 2
yourImageView.layer.cornerRadius = radius
yourImageView.layer.masksToBounds = true
© www.soinside.com 2019 - 2024. All rights reserved.