0

I am having and imageView and I am getting the userData from the server when loggedIn in the userData I have a parameter for "profilePic":"penguins.jpg". now I am adding the domain like "http://.....(self.userData.value(forKey: "profilePic")!)" and saving this value into a string variable to store and when I am accessing the value and trying to convert the url into data and adding to imageView.image it is throwing : unexpectedly found nil while unwrapping an optional value..

All my other userData like name,address,phoneNumber are showing fine except for Image.

My Code:

here are the some of the many ways I tried:

way:1

let ad : AppDelegate = UIApplication.shared.delegate as! AppDelegate

let imageUrlString = ad.userImagePath
let imageUrl:URL = URL(string: imageUrlString)! // it is throwing error here(: unexpectedly  found nil while unwrapping an optional value)

DispatchQueue.global(qos: .userInitiated).async {

    let imageData:NSData = NSData(contentsOf: imageUrl)!

    DispatchQueue.main.async {
        let image = UIImage(data: imageData as Data)
        self.profileImage.image = image
    }
}

way:2

if let url = NSURL(string: ad.userImagePath) {
            if let data = NSData(contentsOf: url as URL){
                if let imageUrl = UIImage(data: data as Data) {
                    profileImage.image = imageUrl
                }
            }
        }

I have tried different ways to solve this, can't figure out what is my mistake.. finally I am here.. Please someone help he...

  • Duplicate of: https://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift ? – Egghead Apr 06 '18 at 08:03
  • Because of `"profilePic":"penguins.jpg"` as mean your `imageUrlString` is `penguins.jpg`. That's a string, not url, so it will return nil – Quoc Nguyen Apr 06 '18 at 08:04
  • @Quoc Nguyen I am doing "let imageUrl:URL = URL(string: imageUrlString)!" to convert it into url... so, it should convert right??? – Venkatesh Chejarla Apr 06 '18 at 08:09
  • @VenkateshChejarla : You want to download the `penguins.jpg` to your app or your app has it before – Quoc Nguyen Apr 06 '18 at 08:20
  • @Quoc Nguyen : I want to download the image.. – Venkatesh Chejarla Apr 06 '18 at 08:28
  • @VenkateshChejarla: You must to ask your server return the image as in a url link, not image name. The response should be "profilePic":"https://www.example.com/penguins.jpg" – Quoc Nguyen Apr 06 '18 at 08:29
  • Possible duplicate of [Loading/Downloading image from URL on Swift](https://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift) – Shivam Tripathi Apr 06 '18 at 08:33
  • @Quoc Nguyen : you can see my question just updated I am adding the that string to the "profilePic" in the dictionary by ad.userImagePath = "http://domainName.com/app/Users/ProfilePics(self.userData.value(forKey: "profilePic")!)" – Venkatesh Chejarla Apr 06 '18 at 08:34
  • @VenkateshChejarla try this `ad.userImagePath = "domainName.com/app/Users/ProfilePics" + "\(self.userData.string(forKey: "profilePic")!)" ` – Quoc Nguyen Apr 06 '18 at 08:46
  • @Quoc Nguyen : I am getting error saying "value of type NSDictionary has no member string"... – Venkatesh Chejarla Apr 06 '18 at 09:25
  • @Quoc Nguyen : And I think there is no need to use + for concatenation as I am doing it right..or I think so.. – Venkatesh Chejarla Apr 06 '18 at 09:35
  • @VenkateshChejarla : Dont force unwrap the `self.userData.string(forKey: "profilePic")`. Check it. If it return nil, that mean you has not save it before. Let save it. The next time you can get it – Quoc Nguyen Apr 06 '18 at 09:49
  • @Quoc Nguyen : mate you don't get it... it is throwing error saying there is no such property "string" for type NSDictionary. So, this `self.userData.string(forKey:"profilePic")` and that is like wrong syntax.. I am using swift 4. May be that is the reason – Venkatesh Chejarla Apr 07 '18 at 04:30

0 Answers0