简体   繁体   中英

How can I show multiple image url string to UIImageView using slider in swift

I am new in swift and I am facing problem to show multiple url string image to uiimage

my code is like this

In 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]
    }

Now I am getting url string like this

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

How can I show this urls in image view ?

I am using this code to convert url to image but it getting crash now

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

First you need to get the image urls from the string by using this

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: " , ")

The value of imageArr will be like

["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"]

Now you have the individual image URL. Download and show image in individual index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM