如何正确地设置imageView中像imageContacts一样的圆形?

问题描述:

我想展示的图片到像图像接触的ImageView(一圈),但是当我尝试证明这一点,该ImageView的重新调整他的大小和这个不转了一圈正确显示..如何正确地设置imageView中像imageContacts一样的圆形?

image.layer.borderWidth=1.0 
image.layer.masksToBounds = false 
image.layer.borderColor = UIColor.whiteColor().CGColor 
image.layer.cornerRadius = image.frame.size.height/2 
image.clipsToBounds = true 

我要显示这样的: enter image description here

但我得到这个: enter image description here

如何能做到图像调整到UIImageView的大小来显示像圈?

谢谢!

+0

这里是身高的情况下,解决方案的ImageView有宽度不同:http://stackoverflow.com/questions/29685055/ ios-frame-size-width-2-doesnt-produce-a-circle-on-each-device – lee 2016-08-18 04:29:22

什么frame大小你用于image?如果将框架设置为正方形,我可以获得完美的圆形。

let image = UIImageView(frame: CGRectMake(0, 0, 100, 100)) 
+0

我在大小检查器中的图像大小设置为120宽度和120高度,但是当我运行它时,它不是一个完美的因为宽度变得更长了一圈?为什么? – 2016-01-21 19:08:58

+0

如果框架是正方形,将半径设置为宽度/高度的一半将始终为您提供圆形视图。如果创建的自定义类可能符合各种方框大小,则不能对半径值进行硬编码。 – Mahouk 2016-02-27 06:44:20

这是解决方案,我在我的应用程序都使用:

var image: UIImage = UIImage(named: "imageName") 
imageView.layer.borderWidth = 1.0 
imageView.layer.masksToBounds = false 
imageView.layer.borderColor = UIColor.whiteColor().CGColor 
imageView.layer.cornerRadius = image.frame.size.width/2 
imageView.clipsToBounds = true 

雨燕4.0

let image = UIImage(named: "imageName") 
imageView.layer.borderWidth = 1.0 
imageView.layer.masksToBounds = false 
imageView.layer.borderColor = UIColor.white.cgColor 
imageView.layer.cornerRadius = image.frame.size.width/2 
imageView.clipsToBounds = true 
+0

您能否为swift 3做一个更新? – Antonio 2017-04-22 23:22:47

+0

为什么这里的所有答案都如此投票 - 如此投票呢?是不是有点愚蠢的使用cornerRadius有图像不必是方形? – Injectios 2017-11-09 11:08:24

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

 profilepic = UIImageView(frame: CGRectMake(0, 0, self.view.bounds.width * 0.19 , self.view.bounds.height * 0.1)) 
     profilepic.layer.borderWidth = 1 
     profilepic.layer.masksToBounds = false 
     profilepic.layer.borderColor = UIColor.blackColor().CGColor 
     profilepic.layer.cornerRadius = profilepic.frame.height/2 
     profilepic.clipsToBounds = true 
     slider.addSubview(profilepic) 
+0

你需要为此导入'''quartzcore'''吗? – Supertecnoboff 2015-10-23 13:19:21

+0

@Supertecnoboff nope。! – 2015-11-18 18:43:25

我建议让你的图像文件成为一个完美的方块。这可以在几乎任何照片编辑程序中完成。之后,这应该在viewDidLoad内工作。感谢这个video

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

快速简单解决方案。

如何使用遮罩UIImage使用Swift无需修剪即可。

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 = anyImage 
    } 
} 

如何拨打:

let anyAvatarImage:UIImage = UIImage(named: "avatar")! 
avatarImageView.maskCircle(anyAvatarImage) 
+1

我找不到'prepareImage()'方法。它被改变了吗? – Antonio 2017-04-22 23:18:03

+0

@Antonio,这是他的自定义功能...你只是使用self.image = anyImage – 2017-06-08 11:56:56

+0

我删除了额外的功能。谢谢@BorisNikolić – fatihyildizhan 2017-06-09 12:17:56

我固定它做修改视图:

  1. 转到您的Main.storyboard
  2. 点击图像
  3. 上查看 - >模式 - >方面填充

它完美

这项工作非常适合我。 行的顺序是重要

func circularImage(photoImageView: UIImageView?) 
{ 
    photoImageView!.layer.frame = CGRectInset(photoImageView!.layer.frame, 0, 0) 
    photoImageView!.layer.borderColor = UIColor.grayColor().CGColor 
    photoImageView!.layer.cornerRadius = photoImageView!.frame.height/2 
    photoImageView!.layer.masksToBounds = false 
    photoImageView!.clipsToBounds = true 
    photoImageView!.layer.borderWidth = 0.5 
    photoImageView!.contentMode = UIViewContentMode.ScaleAspectFill 
} 

如何使用:

@IBOutlet weak var photoImageView: UIImageView! 
    ... 
    ... 
    circularImage(photoImageView) 

我发现了什么是当除以2,您的宽度和图像视图的高度必须返回一个偶数得到一个完美的圆如

let image = UIImageView(frame: CGRectMake(0, 0, 120, 120)) 

它不应该是这样的 让图像= UIImageVi ew(frame:CGRectMake(0,0,130,130))

reviewerImage.layer.cornerRadius = reviewerImage.frame.size.width/2; 
reviewerImage.clipsToBounds = true 

这也适用于我。要获得完美的圆形效果,请在宽度和高度上使用相同的尺寸。像image.frame = CGRect(0,0,200, 200)

对于非完美的圆形,宽度和高度不应该像下面的代码一样。

image.frame = CGRect(0,0,200, 160) 
image.layer.borderColor = UIColor.whiteColor().CGColor 
image.layer.cornerRadius = image.frame.size.height/2 
image.layer.masksToBounds = false 
image.layer.clipsToBounds = true 
image.contentMode = .scaleAspectFill 

我有一个类似的结果(更多的椭圆比圆)。事实证明,我在UIImageView上设置的限制迫使它变成椭圆形而不是圆形。解决之后,上面的解决方案工作。

创建您的自定义圆圈UIImageView并在layout下创建圆圈如果您使用Autolayout,则可以帮助您创建圆圈。

/* 
     +-------------+ 
     |    | 
     |    | 
     |    | 
     |    | 
     |    | 
     |    | 
     +-------------+ 
    The IMAGE MUST BE SQUARE 
*/ 
class roundImageView: UIImageView { 

    override init(frame: CGRect) { 
     // 1. setup any properties here 
     // 2. call super.init(frame:) 
     super.init(frame: frame) 
     // 3. Setup view from .xib file 
    } 

    required init?(coder aDecoder: NSCoder) { 
     // 1. setup any properties here 
     // 2. call super.init(coder:) 
     super.init(coder: aDecoder) 
     // 3. Setup view from .xib file 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 
     self.layer.borderWidth = 1 
     self.layer.masksToBounds = false 
     self.layer.borderColor = UIColor.white.cgColor 
     self.layer.cornerRadius = self.frame.size.width/2 
     self.clipsToBounds = true 
    } 
} 

试试这个。 迅速代码

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(true) 
    perform(#selector(self.setCircleForImage(_:)), with: pickedImage, afterDelay: 0) 
} 


@objc func setCircleForImage(_ imageView : UIImageView){ 
    imageView.layer.cornerRadius = pickedImage.frame.size.width/2 
    imageView.clipsToBounds = true 
} 

使用此代码,以使图像一轮

 self.layoutIfNeeded() 
    self.imageView.layer.cornerRadius = 
    self.imageView.frame.width/2 
    self.imageView.layer.masksToBounds = true