Kingfisher: How to start/stop/pause GIFs

Created on 2 Jan 2016  路  1Comment  路  Source: onevcat/Kingfisher

After setting a GIF URL like so: gifImageView.kf_setImageWithURL(gifURL) how can you pause it?

ImageView does not respond anymore to stopAnimating() and startAnimating() functions.

Most helpful comment

Kingfisher is using the image property directly for setting a GIF image to image view. So the animated image is not a sequence of images. It is not be played by startAnimating API, so you could not stop it by stopAnimating as well.

A workaround for it is adding a wrap to convert the single animated image to an array of images and then play it with startAnimating in the image view set method completion handler. Like this:

imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: nil,
    completionHandler: {
        image, error, cacheType, imageURL in
        if let image = image {
            imageView.animationImages = image.images
            imageView.animationDuration = image.duration
            imageView.animationRepeatCount = 0
            imageView.image = image.images?.last
            imageView.startAnimating()
        }
    })

Then, you can call stopAnimating on the image view to stop it when you need:

imageView.stopAnimating()

I hope it could help.

>All comments

Kingfisher is using the image property directly for setting a GIF image to image view. So the animated image is not a sequence of images. It is not be played by startAnimating API, so you could not stop it by stopAnimating as well.

A workaround for it is adding a wrap to convert the single animated image to an array of images and then play it with startAnimating in the image view set method completion handler. Like this:

imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: nil,
    completionHandler: {
        image, error, cacheType, imageURL in
        if let image = image {
            imageView.animationImages = image.images
            imageView.animationDuration = image.duration
            imageView.animationRepeatCount = 0
            imageView.image = image.images?.last
            imageView.startAnimating()
        }
    })

Then, you can call stopAnimating on the image view to stop it when you need:

imageView.stopAnimating()

I hope it could help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iLucas97 picture iLucas97  路  5Comments

wudijimao picture wudijimao  路  3Comments

indrajitsinh picture indrajitsinh  路  5Comments

freak4pc picture freak4pc  路  3Comments

litt1e-p picture litt1e-p  路  4Comments