gpt4 book ai didi

ios - 如何在swift中将图像设置为圆形

转载 作者:IT王子 更新时间:2023-10-29 04:58:03 25 4
gpt4 key购买 nike

我怎样才能用 swift 圈出图片?

我的 View Controller :

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))
}
}

我的 profilPicture = UIImageView(frame: CGRectMake(0, 0, 100, 100)) 什么都不做..

示例:http://www.appcoda.com/ios-programming-circular-image-calayer/

最佳答案

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
}

如果你想在扩展上使用它

import UIKit

extension UIImageView {

func makeRounded() {

layer.borderWidth = 1
layer.masksToBounds = false
layer.borderColor = UIColor.black.cgColor
layer.cornerRadius = self.frame.height / 2
clipsToBounds = true
}
}

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

关于ios - 如何在swift中将图像设置为圆形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28074679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com