2

thanks for taking the time to read my question.

i have an app project using swift that has a function using sdwebimage. This function is pretty simple and works perfectly in my code.

However, this function is called many times throughout my app and can be slightly messy in code.

I would like to create a swift extension file that can be accessed every time a UIimageView.image needs to be downloaded from my database.

my current download code is:

usersImage1 = the uiimageview to be set.

user.imageOne = the image URL string from firebase

                usersImage1.sd_setImage(with: URL(string: user.imageOne!)) { (image, error, cache, urls) in

                    if (error != nil) {

                        self.usersImage1.image = UIImage(named: "1")

                    } else {

                        self.usersImage1.image = image

                    }
                } 

i am looking for something like the following:

An extension that can be called where uiimageview and imageurl can be replaced when the function is called with the required URL string and uiimageview.

extension UIImageView {

func loadImageFromDatabase(image: UIImageView, imageUrl: string) {

    image.sd_setImage(with: URL(string: imageUrl!)) { (image, error, cache, urls) in

        if (error != nil) {

            self.image.image = UIImage(named: "1")

        } else {

            self.image.image = image

        }
    }

}

}

Then in viewcontroller use the function like this:

self.userimage1.loadImageFromDatabase(imageUrl)

thanks in advance for your help.

DrDev
  • 23
  • 4
  • Seems like you are trying to handle the scenario when you get an error and using a local image in that case. Have you already tried using the *placeholder* argument made available by another function from SDWebImage? – Kushal Ashok Jan 09 '19 at 19:20
  • It’s more of a tidy up than anything else. This is called through my app many time. But if I can figure a way to use an extension I can also make an extension similar for the upload of an image to firebase aswell – DrDev Jan 09 '19 at 19:22
  • You just want the way to use an extension? https://stackoverflow.com/questions/33942483/swift-extension-example – Kushal Ashok Jan 09 '19 at 19:27

1 Answers1

1

If I have understood your desire correctly, you can achieve this with a manager class instead of extension, which will be responsible for downloading image for you. Create a class like below:

class ImageDownloaderManager {
    class func loadImageFromDatabase(userImage: UIImageView, imageUrl: String, completionHandler: @escaping (Bool)-> Void) {

        image.sd_setImage(with: URL(string: imageUrl!)) { (image, error, cache, urls) in
            if (error != nil) {
                userImage.image = UIImage(named: "1") // set your placeholder image maybe.
                completionHandler(false)
            } else {
                userImageimage = image
                completionHandler(true)
            }
        }
        completionHandler(true)
    }
}

It has a completion block where you can understand if your image was downloaded successfully or not from where-ever you call this function. Lets say you call this method from a viewController's viewDidLoad method as following:

override func viewDidLoad() {
    super.viewDidLoad()
    ImageDownloaderManager.loadImageFromDatabase(image: yourImage, imageUrl: yourImageURL) { (isSucceeded) in
        if isSucceeded {
            // It was successful, you could download the image successfully, do smth
        } else {
            // It was not successful image couldnt be downloaded, placeholder image has been set.
        }
    }
}
emrepun
  • 2,496
  • 2
  • 15
  • 33
  • Hi thanks for your example. I am getting - Cannot assign to value: 'image' is a 'let' constant. – DrDev Jan 12 '19 at 15:40
  • Sorry, I made some changes can you check now? You will return an actual UIImage from the call if it succeeds. – emrepun Jan 12 '19 at 15:50
  • I found the solution see below. if you alter your answer back to what is was. all i changed was the Uimage to uiimageview in the function handlers and then set image.image. and it works perfect. thanks for your fast repy and great answer. – DrDev Jan 12 '19 at 15:52
  • I rolled back to my previous edit, can you edit and make the changes then I can approve your edits? – emrepun Jan 12 '19 at 15:54
  • I made the edits from your suggestion. Glad it works now. – emrepun Jan 12 '19 at 15:57
  • edited. made it slightly clearer instaid of image.image changed to userImage – DrDev Jan 12 '19 at 15:58