0

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)
        }
Mujtaba
  • 97
  • 1
  • 7

1 Answers1

1

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.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50