Swift - Image Download issue

144 views Asked by At

Good morning !

I have a design question regarding pictures downloading and constraints.

I currently use Haneke to download my images in the background. It is working like a charm but I have a lots of different image size format I can have (I display one image at a time).

So my problem is that I tweak the image depending on it's ratio in order to accomodate the different sizes. It is working quite well :

 if let url = NSURL(string: coverPhoto.url) {

            self.coverImageView?.hnk_setImageFromURL(url,format: nil, failure: nil,success: { image in

                // Screen Width
                let screen_width = UIScreen.mainScreen().bounds.width

                // Ratio Width / Height
                let ratio =  image.size.height / image.size.width

                // Calculated Height for the picture
                let newHeight = screen_width * ratio

                self.__coverImageViewHeightLayoutConstraint?.constant = newHeight

                self.coverImageView?.image = image
            })
        }

I use Prototype cell to display my content. So when that code is done, I reload the Cover Image cell in order to apply / update the new layout. So far so good !!

But my problem is when the image does not look good at all and it is being resized, it takes a little bit of time for the new constraint to be applied, which lead to a weird / not good looking UI because you can see it happening :=/

The other way around that I am using is to simply load the image synchronically :

                if let dataUrl = NSData(contentsOfURL: url)
            {
                let image = UIImage(data: dataUrl)

                // Screen Width
                let screen_width = UIScreen.mainScreen().bounds.width

                // Ratio Width / Height
                let ratio =  image!.size.height / image!.size.width

                // Calculated Height for the picture
                let newHeight = screen_width * ratio

                self.__coverImageViewHeightLayoutConstraint?.constant = newHeight

                self.coverImageView!.image = image!
            }

But obviously I would like to avoid that for performance reason / scrolling issues (the scrolling back up will download the image again since it is not cached).

What is the best approach is there is one ?

Thank you !

0

There are 0 answers