使UIImageView四舍五入

时间:2016-01-12 18:09:48

标签: ios swift uiimageview

我希望我的图像显示为一个完整的360度圆圈。当我构建并运行我的图像时显示为椭圆形。 This post

@IBOutlet var profileImageView: UIImageView!

override func viewDidLoad() {
  super.viewDidLoad()
  profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2
  profileImageView.clipsToBounds = true
  profileImageView.layer.borderColor = UIColor.whiteColor() .CGColor
  profileImageView.layer.borderWidth = 3

  picker.delegate = self
  picker.dataSource = self
  milesTextField.inputView = picker            
}

5 个答案:

答案 0 :(得分:2)

您似乎使用了autolayout。当您尝试设置cornerRadius时,您的width viewDidLoad还没有完全准备就绪。在viewDidLayoutSubviews方法布局尚未准备好。您应该将代码放到override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2 profileImageView.clipsToBounds = true profileImageView.layer.borderColor = UIColor.whiteColor() .CGColor profileImageView.layer.borderWidth = 3 } 方法。

$httpBackend.expectPost

答案 1 :(得分:1)

enter image description here

如果您使用自动布局,则需要为imageview提供1:1约束。然后你的代码就好了。

选择图像并单击storybaord中的图钉以添加约束,然后选择纵横比约束,默认情况下它将具有3:4或4:3等比例,您需要将其作为 1:1

注意:不要为imageview保留任何高度/宽度约束。只需给出前导,尾随,顶部,底部和1:1比率约束。

答案 2 :(得分:0)

转到Identity inspector,单击用户定义的运行时属性编辑器左下角的Add button (+)。双击新属性的Key Path字段,将属性的关键路径编辑为layer.cornerRadius将类型设置为Number,将值设置为30。要从方形图像制作圆形图像,半径设置为图像视图宽度的一半。或者您可以添加此代码,我发现第一种方法更容易,但您可以决定使用哪种代码。

// 30 is an example you can do the math and divide the width of the image
profileImageView.layer.cornerRadius = 30.0 
profileImageView.clipsToBounds = true

答案 3 :(得分:0)

如果您的UIImageView不是正方形,则可能无法使用UIImageView.layer.cornerRadius将UIImageView转换为圆形。

替代
可以将图像转换为圆形,然后将其添加到UIImageView。

此处代码在将其添加到UIImageView之前创建Circle图像。

class ViewController: UIViewController {

    @IBOutlet var imgView: UIImageView!
    let imgTest = UIImage(named: "Lion.jpg")!

    override func viewDidLoad() {
        super.viewDidLoad()

        imgView.image = circleImage(imgTest)
    }

    func circleImage(image: UIImage) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(image.size, false, 1.0)

        let q = min(image.size.width, image.size.height)
        let bp = UIBezierPath(arcCenter: CGPoint(x: image.size.width/2, y: image.size.height/2), radius: q/2, startAngle: CGFloat(0), endAngle: CGFloat(M_PI) * 2, clockwise: true)
        bp.addClip()
        image.drawInRect(CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))

        let imgLast = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return imgLast
    }

}

答案 4 :(得分:0)

如果您希望舍入每个UIImageView,可以将这段代码复制到您的项目中,而不必忘记选中clip to bounds并将其值设置为true

import UIKit

@IBDesignable
extension UIImageView
{
    private struct AssociatedKey
    {
        static var rounded = "UIImageView.rounded"
    }

    @IBInspectable var rounded: Bool
    {
        get
        {
            if let rounded = objc_getAssociatedObject(self, &AssociatedKey.rounded) as? Bool
            {
                return rounded
            }
            else
            {
                return false
            }
        }
        set
        {
            objc_setAssociatedObject(self, &AssociatedKey.rounded, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            layer.cornerRadius = CGFloat(newValue ? 1.0 : 0.0)*min(bounds.width, bounds.height)/2
        }
    }
}

storyboard screenshot