I need to know when webp Image, which is downloaded form internet, finished animation. However, the loopCompletionBlock has never be called
private let imgView = FLAnimatedImageView();
imgView .sd_setImage(with: URL(string: urlLink));
imgView .loopCompletionBlock = { count in
dbLog("test count") // not be called ?
}
PS: I downloaded images before
let prefetcher = SDWebImagePrefetcher.init()
prefetcher.prefetchURLs(urlStrs.flatMap { return $0 }, progress: nil, completed: nil)
Animated WebP is just a UIAnimatedImage when using with FLAnimatedImageView, it's not their class FLAnimatedImage so they just call super method to let UIImageView maintain the animation and they does not do anything in their own implementation. That's why their own loopCompletionBlock not work. You can request feature for FLAnimatedImage repo.
Current there is no good way to KVO one loop finished for UIImageView. If your image just loop once, you can specify 1 for animationRepeatCount and KVO animating property on UIImaegView, there is the only way for build-in property.
Moreover, In 5.x we have SDAnimatedImageView and we will move FLAnimatedImageView+WebCache into another third-party cocoapods instead of our official repo. SDAnimatedImageView need a SDAnimatedImage protocol instance which is a subclass of UIImage/NSImage and support custom coder, the animated image can be GIF, APNG, Animated WebP images or even from your own custom coder support format. It supports you to KVO two property currentLoopCount and animating to know the context
This can solve these problem because it's written by ourself instead of buld-in imageView class from Apple. See more in #2140
Thanks @dreampiggy,
I set
imgView.animationRepeatCount = 1
However, my webp animation doesn't stop, it loops forever :(. May you help me to give the example for using KVO to watch animating property on UIImageView ? :( Thanks very much Y_Y
Therefore, When is the release of branch 5.x?
@bembem1011 That's Apple's API design issue.
If you use a UIImage create by -[UIImage animatedImageWithImages:duration:](This acutall class is UIAnimatedImage). Then set this UIAnimatedImage to UIImageView. It loops forever(really strange but UIKit adopt this behavior) :)
If you want to control the loop count. You should write this code instead:
self.imageView.image = image.images.firstObject; // This will cause the animation stop at the first frame. You can also use lastObject if you need to stop at last frame.
self.imageView.animationRepeatCount = image.sd_imageLoopCount; // Sepcify animation loop count, for your case it should be 1
self.imageView.animationDuration = image.duration;
self.imageView.animationImages = image.images;
[self.imageView startAnimating];
Then, add KVO on self.imageView.animating, you will get KVO called after that loop count finished.
[self.KVOController observe:self.imageView keyPath:NSStringFromSelector(@selector(animating)) options:NSKeyValueChangeNewKey block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
BOOL animating = [object boolValue];
if (!animating) {
// Means loop finished. Do something
}
}];
Hi @dreampiggy
Will this issue be handled in branch 5.x's SDAnimatedImageView ? May you tell me the 5.x's final release time ?
Thanks very much :(
The release date maybe April, but maybe there is a beta version first(5.0-beta), and we should fix some bugs or wait for any other important API-break changes.
The 5.x may not break something like 4.x. For the core API like sd_setImageWithURL, nothing changed. So feel free to update :)
The biggest changes are mostlly on advanced feature like custom coder(The protocol may change a bit), custom transformer(Use a id<SDWebImageTransformer> instead of delegate method), custom cache(No big API change). The changes are all here in 5.x milestone
Thanks very much @dreampiggy