ios 如何在Swift中正确设置像imageContacts这样的圆圈中的imageView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25587713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set imageView in circle like imageContacts in Swift correctly?
提问by user3745888
I want to show a picture into imageView like the image contact (in a circle) But when I try to show this, the imageView rescale his size and this doesn't show correctly in a circle.
我想将图片显示到 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
I want to show like this:
我想这样显示:
But I get this:
但我明白了:
How can do the image resize to UIImageView size to show as a circle?
如何将图像调整为 UIImageView 大小以显示为圆形?
Thanks!
谢谢!
采纳答案by chuckus
What frame
size are you using for image
? I can get a perfect circle if I set the frame to be a square.
frame
你用什么尺码image
?如果我将框架设置为正方形,我可以获得一个完美的圆形。
let image = UIImageView(frame: CGRectMake(0, 0, 100, 100))
回答by Manu Gupta
This is solution which I have used in my app:
这是我在我的应用程序中使用的解决方案:
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
斯威夫特 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
回答by fatihyildizhan
Fastand Simplesolution.
快速和简单的解决方案。
How to maskUIImage
to Circle without cropping with Swift.
如何在不使用 Swift 裁剪的情况下屏蔽UIImage
圆形。
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
}
}
How to call:
如何调用:
let anyAvatarImage:UIImage = UIImage(named: "avatar")!
avatarImageView.maskCircle(anyAvatarImage)
回答by iluvatar_GR
Create your custom circle UIImageView and create the circle under the layoutSubviews helps if you use 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
}
}
回答by Jaywant Khedkar
Try this it's work for me ,
试试这个对我有用,
set imageViewwidth and height same .
设置imageView 的宽度和高度相同。
Swift
迅速
imageview?.layer.cornerRadius = (imageview?.frame.size.width ?? 0.0) / 2
imageview?.clipsToBounds = true
imageview?.layer.borderWidth = 3.0
imageview?.layer.borderColor = UIColor.white.cgColor
Objective C
目标 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;
Hope this will help to some one .
希望这将有助于某人。
回答by jsanabria
I would suggest making your image file a perfect square to begin with. This can be done in almost any photo editing program. After that, this should work within viewDidLoad. Credit to this video
我建议首先让您的图像文件成为一个完美的正方形。这几乎可以在任何照片编辑程序中完成。之后,这应该在 viewDidLoad 中工作。归功于此视频
image.layer.cornerRadius = image.frame.size.width/2
image.clipsToBounds = true
回答by Codetard
That is all you need....
这就是你所需要的......
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
回答by Lance Samaria
If your using a UIViewController
here's how do do it using Anchors. The key is to set the imageView's layer.cornerRadius
in viewWillLayoutSubviews
like so:
如果您使用UIViewController
这里是如何使用Anchors。关键是要设置的ImageViewlayer.cornerRadius
中viewWillLayoutSubviews
像这样:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
imageView.layer.cornerRadius = imageView.frame.size.width / 2
}
Also make sure the heightAnchor
and widthAnchor
are the same size. They are both 100
in my example below
还要确保heightAnchor
和widthAnchor
的大小相同。他们都100
在我下面的例子中
Code:
代码:
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() {
super.viewWillLayoutSubviews()
imageView.layer.cornerRadius = imageView.frame.size.width / 2
}
If your using a CollectionView Cell
set the imageView's layer.cornerRadius
in layoutSubviews()
:
如果您使用CollectionView Cell
set imageView's layer.cornerRadius
in 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
}
回答by Sai kumar Reddy
reviewerImage.layer.cornerRadius = reviewerImage.frame.size.width / 2;
reviewerImage.clipsToBounds = true
回答by Akhil Clement
try this. swiftcode
尝试这个。 快速代码
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
}