首页 > 解决方案 > 如何在 swift 中使用滑块向 UIImageView 显示多个图像 url 字符串

问题描述

我是 swift 新手,我面临向 uiimage 显示多个 url 字符串图像的问题

我的代码是这样的

在 viewdidLoad

Array = [UIImage (named: "ISDI1")!,UIImage (named: "ISDI2")!,UIImage (named: "ISDI3")!,UIImage (named: "ISDI4")!,UIImage (named: "ISDI5")!]
        sliderView.maximumValue = Float(Array.count - 1)



  @IBAction func btnPreviousClick(_ sender: Any) {
        sliderView.value -= 1
        imgView.image = Array[Int(sliderView.value)]
    }

    @IBAction func btnNextClick(_ sender: Any) {
        sliderView.value += 1
        imgView.image = Array[Int(sliderView.value)]
    }

    @IBAction func SliderViewClick(_ sender: UISlider) {
        var value = Int(sender.value)
        imgView.image = Array[value]
    }

现在我得到这样的url字符串

http://diceapp.in/mailer_isdi_2020/images/4.jpeg , http://diceapp.in/mailer_isdi_2020/images/3.jpeg , http://diceapp.in/mailer_isdi_2020/images/1.jpeg

如何在图像视图中显示此网址?

我正在使用此代码将 url 转换为图像,但它现在崩溃了

let url = NSURL(string:Image ?? "")
        if !(url?.absoluteString == "") {
            let data = NSData(contentsOf: url! as URL) //make sure your image in this url does exist, otherwise unwrap in a if let check
            imgView.image = UIImage(data: data! as Data)
        }

标签: iosarraysswift

解决方案


首先,您需要使用此字符串从字符串中获取图像 url

var imageUrlStrings = "http://diceapp.in/mailer_isdi_2020/images/4.jpeg , http://diceapp.in/mailer_isdi_2020/images/3.jpeg , http://diceapp.in/mailer_isdi_2020/images/1.jpeg"
var imageArr = imageUrlStrings.components(separatedBy: " , ")

的值imageArr将像

["http://diceapp.in/mailer_isdi_2020/images/4.jpeg", "http://diceapp.in/mailer_isdi_2020/images/3.jpeg", "http://diceapp.in/mailer_isdi_2020/images/1.jpeg"]

现在您有了单独的图像 URL。下载并在单个索引中显示图像。


推荐阅读