SwiftTry
SwiftTry

Reputation: 151

How to load images dynamically inside UITextView

I have a UITextView and appending TextAttachments inside of it. I am getting photos from web url. Here is the code:

image.load(url: URL(string: ("http://www.furkan.webofisin.com/upload/media/5db1b96366cd8.jpg")))
                        imageAttachment.image = image.image

                    let image1String = NSAttributedString(attachment: imageAttachment)
                    fullString.append(image1String)

And this is the load function as extension:

var imageUrlString: String?

func load(urlString: String) {

    imageUrlString = urlString
    guard let url = URL(string: urlString) else {

        DispatchQueue.main.async {
            let urlMessage = "Hata"
            appDelegate.errorView(message: urlMessage, color: smoothGreenColor)
        }

        return

    }

    image = nil

    if let imageFromCache = imageCache.object(forKey: urlString as NSString) {

        self.image = imageFromCache
        return
    }

    URLSession.shared.dataTask(with: url) { (data , response, error) in

        if error != nil {

               DispatchQueue.main.async {
                     let urlMessage = "Bir hata meydana geldi."
                     appDelegate.errorView(message: urlMessage, color: smoothGreenColor)
                     }
            return
        }

       DispatchQueue.main.async {

            let imageToCache = UIImage(data: data!)

            if self.imageUrlString == urlString {

                self.image = imageToCache
            }

             imageCache.setObject(imageToCache ?? UIImage(named: "astast")!, forKey: urlString as NSString)

       }

    }.resume()

}

Images not displaying at first time after refresh page its displaying because of caching. I checked image.image as UIImage but its always nil before loading finished.

Here is the screenshot of first opening.

At first opening image data is nil

How can I detect and reload images when image data filled.

Upvotes: 2

Views: 326

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100541

Add a completion as call to get image initially is asynchronous unlike from local cache

func load(urlString: String,completion:@escaping(()  -> () )) {

    imageUrlString = urlString
    guard let url = URL(string: urlString) else {

        DispatchQueue.main.async {
            let urlMessage = "Hata"
            appDelegate.errorView(message: urlMessage, color: smoothGreenColor)
        }

        return

    }

    image = nil

    if let imageFromCache = imageCache.object(forKey: urlString as NSString) {

        self.image = imageFromCache
        completion()
        return
    }

    URLSession.shared.dataTask(with: url) { (data , response, error) in

        if error != nil {

               DispatchQueue.main.async {
                     let urlMessage = "Bir hata meydana geldi."
                     appDelegate.errorView(message: urlMessage, color: smoothGreenColor)
                     }
            return
        }

       DispatchQueue.main.async {

            let imageToCache = UIImage(data: data!)

            if self.imageUrlString == urlString {

                self.image = imageToCache
            }

             imageCache.setObject(imageToCache ?? UIImage(named: "astast")!, forKey: urlString as NSString)

            completion()
       }

    }.resume()

}

Call

image.load(url: URL(string: ("http://www.furkan.webofisin.com/upload/media/5db1b96366cd8.jpg"))) {

            imageAttachment.image = image.image 
            let image1String = NSAttributedString(attachment: imageAttachment)
             fullString.append(image1String)

}

Upvotes: 0

Related Questions