ios 如何快速设置圆圈中的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28074679/
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 image in circle in swift
提问by Pixel
How can i make i circle picture with swift ?
我怎样才能让我用swift圈出图片?
My 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))
}
}
My profilPicture = UIImageView(frame: CGRectMake(0, 0, 100, 100))do nothing ..
我profilPicture = UIImageView(frame: CGRectMake(0, 0, 100, 100))什么都不做..
Exemple: http://www.appcoda.com/ios-programming-circular-image-calayer/
示例:http: //www.appcoda.com/ios-programming-circular-image-calayer/
回答by Jon
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
}
If you want it on an extension
如果你想在扩展上使用它
import UIKit
extension UIImageView {
func makeRounded() {
self.layer.borderWidth = 1
self.layer.masksToBounds = false
self.layer.borderColor = UIColor.black.cgColor
self.layer.cornerRadius = self.frame.height / 2
self.clipsToBounds = true
}
}
That is all you need....
这就是你所需要的......
回答by Daniel Kuta
You can simple create extension:
您可以简单地创建扩展:
import UIKit
extension UIImageView {
func setRounded() {
let radius = CGRectGetWidth(self.frame) / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
}
and use it as below:
并使用它如下:
imageView.setRounded()
回答by MrMins
Based in the answer of @DanielQ
基于@DanielQ 的回答
Swift 4 and Swift 3
斯威夫特 4 和斯威夫特 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
}
}
You can use it in any ViewControllerwith:
您可以在任何情况下使用它ViewController:
imageView.setRounded()
回答by Tom Spee
If you mean you want to make a UIImageView circular in Swift you can just use this code:
如果你的意思是你想在 Swift 中制作一个 UIImageView 循环,你可以使用以下代码:
imageView.layer.cornerRadius = imageView.frame.height / 2
imageView.clipsToBounds = true
回答by Grace
Don't know if this helps anyone but I was struggling with this problem for awhile, none of the answers online helped me. For me the problem was I had different heights and widths set on the image in storyboard. I tried every solution on stack and it turns out it was something as simple as that. Once I set them both to 200 my circle profile image was perfect. This was code then in my VC.
不知道这是否对任何人有帮助,但我在这个问题上挣扎了一段时间,网上的答案都没有帮助我。对我来说,问题是我在情节提要中的图像上设置了不同的高度和宽度。我尝试了堆栈上的所有解决方案,结果证明就是这么简单。一旦我将它们都设置为 200,我的圈子个人资料图片就完美了。这是当时在我的 VC 中的代码。
profileImage2.layer.cornerRadius = profileImage2.frame.size.width/2
profileImage2.clipsToBounds = true
回答by Ghulam Rasool
For Swift 4:
对于Swift 4:
import UIKit
extension UIImageView {
func makeRounded() {
let radius = self.frame.width/2.0
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
}
回答by J. Doe
This way is the least expensive way and always keeps your image view rounded:
这种方式是最便宜的方式,并且始终保持您的图像视图四舍五入:
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
}
}
The other answers are telling you to make views rounded based on frame calculations set in a UIViewControllers viewDidLoad()method. This isn't correct, since it isn't sure what the final frame will be.
其他答案告诉您根据UIViewControllersviewDidLoad()方法中设置的帧计算使视图四舍五入。这是不正确的,因为它不确定最终帧会是什么。
回答by Nazar Medeiros
All the answers above couldn't solve the problem in my case. My ImageViewwas placed in a customized UITableViewCell. Therefore I had also call the layoutIfNeeded()method from here. Example:
以上所有答案都无法解决我的问题。我的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;
}
回答by Michal Rogowski
struct CircleImage: View {
var image: Image
var body: some View {
image
.clipShape(Circle())
}
}
This is correct for SwiftUI
这是正确的 SwiftUI
回答by user3182143
First you need to set equal width and height for getting Circular ImageView.Below I set width and height as 100,100.If you want to set equal width and height according to your required size,set here.
首先需要设置等宽和等高才能得到Circular ImageView。下面我设置宽和高为100,100。如果你想根据你需要的大小设置等宽和高度,在这里设置。
var imageCircle = UIImageView(frame: CGRectMake(0, 0, 100, 100))
Then you need to set height/2 for corner radius
然后你需要为拐角半径设置 height/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)

