ZacharyKhan / ZKCarousel

A simple carousel implementation written in Swift
MIT License
289 stars 57 forks source link

Help With Async Image Loading #24

Closed ChrisAldridgeRIT closed 5 years ago

ChrisAldridgeRIT commented 5 years ago

Im pretty sure this is to do with how I've implemented this, rather than the code itself but wasn't sure where else to ask. If its not the right place - sorry.

I drop ZKCarousel into several custom cells in a tableview and get the images using Alamofireimages library from my server. If I scroll quickly through my tableview the images in the ZKCarousel become mixed up or repeated. I'm pretty sure this is related to using dequeueReusableCell and things getting out of sync. Just wondered if you had any advice on how to avoid this problem?

Thanks Chris

ZacharyKhan commented 5 years ago

Hey Chris,

Perfectly fine to ask questions here. You'll find that more often than not, the community is more than happy to help when we can.

I can't say for sure without looking at your code, but I'm willing to bet this issue is related to asynchronously setting image in your imageView. It's a very common mistake that almost everyone makes when they first start building UITableView or UICollectionView.

What may be happening is you are scrolling faster than the time it takes for the image to load. You can get some more info on this topic from some of THESE VIDEOS.

Also, a side note - it may be worth looking into the SDWebImage library. It basically does all the heavy lifting for you.

ChrisAldridgeRIT commented 5 years ago

Many thanks!

ChrisAldridgeRIT commented 5 years ago

Actually I had looked at SDWebimage, but as far as I could see that delivers an UIImageView where as carousel takes just a UIImage unless I'm missing something?

ZacharyKhan commented 5 years ago

Actually I had looked at SDWebimage, but as far as I could see that delivers an UIImageView where as carousel takes just a UIImage unless I'm missing something?

@ChrisAldridgeRIT that is correct, ZKCarousel accepts a UIImage.

But, that UIImage must be displayed via a UIImageView. I have not exposed the UIImageView in ZKCarouselSlide.swift because it is meant to be a simple implementation.

You can go to ZKCarouselSlide and remove the private before the UIImageView to expose the view externally.

From there, you can use SDWebImage to async load images to the image view for each slide. Keep in mind that SDWebImage does not return an additional UIImageView. It simply extends the networking and animation code within functions for UIImageView, which allows users to pass a simple url to an image view and let SDWebImage do the magic.