1

I want to download image and when I run app my app crashed because the image string of json file is like this :

images/product/2021-05-02T09-47-17.699Z-download (2).jpg

there is a distance between download and (2) in my json file and its the problem that make an app crash , in browser its worked but in Xcode not , what's your idea to solve it?

and here is a part of my code when I want to download image :

let imageUrl = URL(string: "http://5.63.13.16:8080/\(item.image.first!)")!
    if let data = try? Data(contentsOf: imageUrl) {
        cellol.offerImageView.image = UIImage(data: data)
    }
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
Ali kazemi
  • 84
  • 7

1 Answers1

-2

you can just use it

    func stringCorrection( _ text:String) -> String{
        return text.replacingOccurrences(of: " ", with: "", options: NSString.CompareOptions.literal, range: nil)
    }

use: -

let imageUrl = URL(string: "http://5.63.13.16:8080/\(stringCorrection(item.image.first!))")!
    if let data = try? Data(contentsOf: imageUrl) {
        cellol.offerImageView.image = UIImage(data: data)
    }
Shivam Parmar
  • 1,520
  • 11
  • 27
  • The crash will not happening with this answer but the image will not show on the imageView. – Faysal Ahmed May 10 '21 at 06:46
  • 1
    yeah thank you so much brother its working now and imageView is showing the image no problem, really useful answer but you need to add "%20" instead of "" in stringCorrection function and its worked, again thanks brother – Ali kazemi May 10 '21 at 06:50
  • @JoakimDanielson yes I agree with you , it is the way that we should learn every thing, but for now I should harry up to solve the project problems, so do you an answer? – Ali kazemi May 10 '21 at 07:00
  • @Alikazemi yes it is in the link I posted to your question above. – Joakim Danielson May 10 '21 at 07:01
  • @JoakimDanielson ok I check out, thanks – Ali kazemi May 10 '21 at 07:04
  • @JoakimDanielson I angry that its not much batter option for learning purpose but its the quick solution. – Shivam Parmar May 10 '21 at 07:05