如何在快速4中进行图像查看

时间:2018-04-12 07:42:10

标签: ios swift uiimageview uiimage swift4

enter image description here虽然互联网上存在这个问题的解决方案,但我无法这样做,我想围绕图像。

我正在使用的代码:

extension UIImageView {        
    func makeRounded() {
        let radius = self.frame.width/2.0
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
    }
 }

然后我在viewdidload()中调用此函数,如imgvw.makeRounded()。但它不会来。请帮忙

上一个链接没有帮助我

3 个答案:

答案 0 :(得分:6)

import UIKit

class ViewController: UIViewController {
  @IBOutlet weak var image: UIImageView!

  func makeRounded() {

    image.layer.borderWidth = 1
    image.layer.masksToBounds = false
    image.layer.borderColor = UIColor.blackColor().CGColor
    image.layer.cornerRadius = image.frame.height/2 //This will change with corners of image and height/2 will make this circle shape
    image.clipsToBounds = true
}

快乐编码

答案 1 :(得分:5)

覆盖viewDidLayoutSubviews将无效调用函数makeRounded(),因为每次调用都会调用superview中的某些布局。你应该用这个:

class RoundedImageView: UIImageView {

    @override func layoutSubviews() {
        super.layoutSubviews()
        let radius = self.frame.width/2.0
        layer.cornerRadius = radius
        clipToBounds = true // This could get called in the (requiered) initializer
        // or, ofcourse, in the interface builder if you are working with storyboards
    }

}

将imageView的类设置为RoundedImageView

答案 2 :(得分:-1)

为您的班级创建扩展

extension ViewController: UIViewController{

    func makeRounded() {

        layer.borderWidth = 1
        layer.masksToBounds = false
        layer.borderColor = UIColor.blackColor().CGColor
        layer.cornerRadius = frame.height/2 
        clipsToBounds = true
    }

}

然后调用它

imageView.makeRounded()