Ios 如何在swift中设置圆形图像
我如何用swift制作我的圆圈图片 我的ViewController:Ios 如何在swift中设置圆形图像,ios,swift,uiimageview,Ios,Swift,Uiimageview,我如何用swift制作我的圆圈图片 我的ViewController: import UIKit import Foundation class FriendsViewController : UIViewController{ @IBOutlet weak var profilPicture: UIImageView! override func viewDidLoad() { super.viewDidLoad() profilPictu
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(帧:CGRectMake(0,0,100,100))
do nothing
示例:如果您想在Swift中制作UIImageView循环,您可以使用以下代码:
imageView.layer.cornerRadius = imageView.frame.height / 2
imageView.clipsToBounds = true
如果你想把它放在分机上
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
}
}
这就是你所需要的一切……我不知道这是否对任何人都有帮助,但我在这个问题上挣扎了一段时间,没有一个在线答案对我有帮助。对我来说,问题是我在故事板的图像上设置了不同的高度和宽度。我尝试了堆栈上的每一个解决方案,结果就是这么简单。一旦我将它们都设置为200,我的圆形轮廓图像就完美了。这是我的VC中的代码
profileImage2.layer.cornerRadius = profileImage2.frame.size.width/2
profileImage2.clipsToBounds = true
首先,您需要设置相等的宽度和高度以获得圆形图像视图。下面我将宽度和高度设置为100100。如果您希望根据所需大小设置相等的宽度和高度,请在此处设置
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)
您可以创建简单的扩展:
import UIKit
extension UIImageView {
func setRounded() {
let radius = CGRectGetWidth(self.frame) / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
}
并按如下方式使用:
imageView.setRounded()
如果您的图像是圆形的,那么它的高度和宽度将完全相同(即120)。您只需取其中的一半并在代码中使用(image.layer.cornerRadius=60)。以上所有答案都无法解决我的问题。我的
ImageView
被放置在定制的UITableViewCell
中。因此,我还从这里调用了layoutifneed()
方法。例如:
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;
}
类名TableViewCell:UITableViewCell,UITextFieldDelegate{。。。
重写func awakeFromNib(){
self.layoutifneed()
profileImageView.layoutIfNeeded()文件
profileImageView.isUserInteractionEnabled=true
let square=profileImageView.frame.size.width
基于@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()
对于Swift3/Swift4开发人员:
let radius = yourImageView.frame.width / 2
yourImageView.layer.cornerRadius = radius
yourImageView.layer.masksToBounds = true
对于Swift 4:
import UIKit
extension UIImageView {
func makeRounded() {
let radius = self.frame.width/2.0
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
}
这种方式是最便宜的方式,并始终保持图像视图的完整性:
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
}
}
其他答案告诉您根据在UIViewController
sviewDidLoad()
方法中设置的帧计算对视图进行取整。这是不正确的,因为它不确定最终帧是什么
// 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!)
这对于SwiftUI来说是正确的“在圆圈中设置图像”是什么意思?你能更准确地描述你想要做什么吗?也许可以画一幅你想要的结果的图片?
profilPicture=UIImageView(frame:CGRectMake(0,0,100,100))
不“什么都不做”。它确实做了些什么!但它所做的是非常不幸的。它创建了一个UIImageView并将其分配给一个弱引用。图像视图是空的;它没有图像。而且,引用是弱的,因此图像视图会立即消失在一股烟雾中。很难从这段代码中看出您想象会发生什么。我想结果是:现在,您添加了一个指向教程的链接,告诉您如何操作。因此,只需按照该教程中的说明操作即可!您已经回答了自己的问题。当我执行教程中的说明时,我会使用swift无需附加:如果您不执行image.clipsToBounds=true
,则图像将无法缩放。@Tom Spee当我设置角半径为图片宽度的一半,我得到的是菱形图像,而不是圆形。你知道为什么会这样吗???@user2363025我怀疑你现在已经弄明白了,但是你确认你使用的是imageView而不是图像了吗?@ScottCorscadden抱歉,斯科特,我没有旧的代码可以回顾。可能有点晚了,但钻石是ape可能是由自动布局引起的,因此您可能需要在之前调用self.view.layoutifneed()
。该image.clipstobunds=true
正是我所需要的:-)如果UIImageView由于约束而增长或收缩,则不起作用。在这种情况下,它必须位于ViewDidLayoutSubview()内我发现在圆形图像周围添加边框会在屏幕上留下一个原始矩形的重影。这在Simulator 10.0上是可见的,在真实设备上可能不会出现,但我认为这足够重要,可以在这里提及。image.layer.borderColor=UIColor.blackColor().CGColor
变成image.layer.borderColor=UIColor.black.CGColor
在swift 4中工作得很好。太好了!谢谢。这应该是答案。两行干净的代码。不需要扩展类或任何额外的内容。不完全是这样,我认为他在为ImageView添加扩展。这是直接更改类的实例。如果不是这样的话有帮助,我可以把它取下来。建议最好添加@IBDesignable
imageView.layer.cornerRadius = imageView.frame.height/2
imageView.clipToBounds = true
struct CircleImage: View {
var image: Image
var body: some View {
image
.clipShape(Circle())
}
}
imageView.layer.cornerRadius = imageView.frame.height/2
imageView.clipToBounds = true