图像未根据尺寸显示

时间:2019-05-28 07:38:38

标签: ios swift imageview

我只想根据图像尺寸显示图像,如果图像高度偏高,则宽度将显示图像,或者如果图像宽度偏高,则其图像将居中显示。

我已经尝试过,并且正在显示我的代码。我在想什么?


override func viewDidLoad() {
        super.viewDidLoad()

        var profileImagesData:Array<UIImage> = []
        for i in 0..<profileImages.count {

       // for venueImageTemp  in profileImages {
            var statusDict = profileImages[i] as! Dictionary<String,Any>
            var imageUrl = String(format:"%@",statusDict["imageId"] as! CVarArg)
            print(imageUrl)

            if imageUrl.contains("cloudinary.com") {
                let imgNameArr = imageUrl.components(separatedBy: "upload/")
                print(imgNameArr)
                let subFisrtStr = imgNameArr[0]
                print(subFisrtStr)
                let subStr = imgNameArr[1]
                print(subStr)
                let subImgNameArr = subStr.components(separatedBy: "/")
                print(subImgNameArr)
                let subNameStr = subImgNameArr[1]
                print(subNameStr)
                imageUrl = ""

                imageUrl = subFisrtStr + "upload/h_600,w_600/" + subNameStr
                print(imageUrl)

            }

            let url = URL(string: imageUrl)
            print(url)
            let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in
            let image = UIImage(data: (data)!)
            profileImagesData.append(image!)
        }

        let imageView = CYCImageScrollView(images: profileImagesData, frame: CGRect(x: 0, y: 20, width: self.view.frame.size.width, height: self.view.frame.size.height-150), pageColor: UIColor.lightGray, currentPageColor: UIColor.orange)
//        imageView.layer.cornerRadius = 30
        imageView.backgroundColor = UIColor.clear

        imageView.contentMode = .scaleAspectFit
        imageView.clipsToBounds = true
        imageView.layer.masksToBounds = true

        self.view.addSubview(imageView)


    }


我只想显示图像的大小。

1 个答案:

答案 0 :(得分:0)

使用内容模式scaleAspectFill代替scaleAspectFit

imageView.contentMode = .scaleAspectFill