我希望将图片显示为imageView,如图像联系人(在一个圆圈中)但是当我尝试显示这个时,imageView会重新调整他的大小,这在圆圈中无法正确显示..
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
我想要这样的节目:
但我明白了:
如何将该图像调整为UIImageView大小以显示为圆圈?
谢谢!
你用frame
的image
大小是多少?如果我将框架设置为正方形,我可以得到一个完美的圆。
let image = UIImageView(frame: CGRectMake(0, 0, 100, 100))
如果你在这里使用UIViewController
是如何使用Anchors做的。关键是在layer.cornerRadius
中设置imageView的viewWillLayoutSubviews
,如下所示:
override func viewWillLayoutSubviews(){
imageView.layer.cornerRadius = imageView.frame.size.width / 2
}
还要确保heightAnchor
和widthAnchor
的大小相同。在下面的例子中,它们都是100
码:
let imageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.borderWidth = 1.0
imageView.clipsToBounds = true
imageView.layer.borderColor = UIColor.white.cgColor
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(imageView)
imageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 50).isActive = true
imageView.image = UIImage(named: "pizzaImage")
}
override func viewWillLayoutSubviews() {
imageView.layer.cornerRadius = imageView.frame.size.width / 2
}
如果你使用CollectionView Cell
在layer.cornerRadius
中设置imageView的layoutSubviews()
:
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(imageView)
imageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
imageView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: self.topAnchor, constant: 50).isActive = true
imageView.image = UIImage(named: "pizzaImage")
}
override func layoutSubviews() {
super.layoutSubviews() // call super.layoutSubviews()
imageView.layer.cornerRadius = imageView.frame.size.width / 2
}
这项工作非常适合我。线的顺序很重要
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))
它不应该像let image = UIImageView(frame:CGRectMake(0,0,130,130))
这对我也有用。要获得完美的圆形结果,请在宽度和高度上使用相同的尺寸。像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
上设置的约束迫使它变成椭圆形而不是圆形。在确定之后,上述解决方案起作用。
使用此代码进行图像舍入
self.layoutIfNeeded()
self.imageView.layer.cornerRadius =
self.imageView.frame.width/2
self.imageView.layer.masksToBounds = true
试试这个对我有用,
设置imageView的宽度和高度相同。
迅速
imageview?.layer.cornerRadius = (imageview?.frame.size.width ?? 0.0) / 2
imageview?.clipsToBounds = true
imageview?.layer.borderWidth = 3.0
imageview?.layer.borderColor = UIColor.white.cgColor
目标C.
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2;
self.imageView.clipsToBounds = YES;
self.imageView.layer.borderWidth = 3.0f;
self.imageView.layer.borderColor = [UIColor whiteColor].CGColor;
希望这对某些人有所帮助。
这个扩展真的对我有用(包括在swift 4+中)
extension UIImageView {
func roundedImage() {
self.layer.cornerRadius = (self.frame.size.width) / 2;
self.clipsToBounds = true
self.layer.borderWidth = 3.0
self.layer.borderColor = UIColor.white.cgColor
}
}
然后简单地称之为
imageView.roundedImage()
这是我在我的应用中使用的解决方案:
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
Swift 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
快速简单的解决方案。
如何将UIImage
隐藏到Circle而不使用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)
如果您使用Autolayout,请创建自定义圆UIImageView并在layoutSubviews帮助下创建圆圈。
/*
+-------------+
| |
| |
| |
| |
| |
| |
+-------------+
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
}
}
这就是你所需要的......
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)
我建议你的图像文件开始时是一个完美的正方形。这几乎可以在任何照片编辑程序中完成。之后,这应该在viewDidLoad中工作。相信这个video
image.layer.cornerRadius = image.frame.size.width/2
image.clipsToBounds = true
reviewerImage.layer.cornerRadius = reviewerImage.frame.size.width / 2;
reviewerImage.clipsToBounds = true
我在修改视图时修复了它:
它完美地运作
试试这个。 SWIFT代码
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
}