NodeJustin
NodeJustin

Reputation: 125

Swift 3 Image download not working fully

So after 5 hours of solid swearing I have finally managed to convert my project from Swift 2.2 to Swift 3. The thing builds and everything is now savvy, with one small exception.

For some reason my images are not Async downloading like they were before.

I have recorded a little video to show you, and some code examples. I am trying both AlamofireImage and just a plain ole Swift Extension, both give the same behaviour. As you can see in the video, initially the images do not load at all (Also I notice for some reason the default placeholder image does not show initially) When I pull to refresh the view all of a sudden they appear.

If anyone has seen anything similar I would appreciate some help if possible.

Video is here

Code examples are thus: Extension functions

 func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
    contentMode = mode
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        guard
            let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
            let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
            let data = data, error == nil,
            let image = UIImage(data: data)
            else { return }
        DispatchQueue.main.async() { () -> Void in
            self.image = image
        }
        }.resume()
}   

func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
    guard let url = URL(string: link) else { return }
    downloadedFrom(url: url, contentMode: mode)
}

Also my use of AlamofireImage for one cell just to test

    if nc.returnCommentDetails().image != "" {
        let url = URL(string: nc.returnCommentDetails().image!)
        cell.imgProfile.af_setImage(withURL: url!)
        cell.imgProfile.resizeForProfilePic()
    }

Upvotes: 2

Views: 1249

Answers (2)

user3126427
user3126427

Reputation: 855

To fix the problem just add this line before the layer.cornerRadius

self.view.layoutIfNeeded()

Then all images showed up with the first load

Upvotes: 1

NodeJustin
NodeJustin

Reputation: 125

So after some soul searching I have found the root cause of the issue.

It basically has an issue with an extension that I wrote for making the image view circular, by commenting that out the images appear fine.

I shall look into this and provide an alternative if anyone is using something similar to this:

func resizeForProfilePic(){
    self.layer.borderWidth = 0.1
    self.layer.masksToBounds = false
    self.layer.borderColor = UIColor.black.cgColor
    self.layer.cornerRadius = self.frame.height/2
    self.clipsToBounds = true
}

Using AlamofireImage seems to have sorted it

let url = URL(string: nc.returnCommentDetails().image!)
let placeholderImage = UIImage(named: "DefaultImage")!
let size = CGSize(width: 100.0, height: 100.0)
let imageFilter = AspectScaledToFillSizeCircleFilter(size: size)
cell.imgProfile.af_setImage(withURL: url!, placeholderImage: placeholderImage, filter: imageFilter)

Upvotes: 2

Related Questions