ibireme / YYWebImage

Asynchronous image loading framework.
MIT License
3.56k stars 615 forks source link

yy_setImageWithURL not working properly with YYAnimatedImageView #15

Closed skyline75489 closed 8 years ago

skyline75489 commented 8 years ago

I'm trying to make a collection view filled with animated GIFs:

The custom cell looks like this:

@interface GifCollectionViewCell : UICollectionViewCell
@property (nonatomic) YYAnimatedImageView *imageView;
@end

And I'm using yy_setImageWithURL to set the image:

if ([cell isKindOfClass:[GifCollectionViewCell class]]) {
    [_cell.imageView yy_setImageWithURL:[self getImageURLForIndexPath:indexPath] options:YYWebImageOptionProgressiveBlur];
}

The images shows but are not animation any more. Just still pictures.

Is this supposed to happen?

ibireme commented 8 years ago

If the image data contains multi-frame, this library will returns YYImage (a subclass of UIImage), the animation can only played in YYAnimateImageView.

If you use [UIImage animatedImageWithImages:] to display animated image, you need to decode all image frames to memory, which may cause memory warning or crash.

skyline75489 commented 8 years ago

I got it. It's just I'm using YYAnimatedImageView, and the name somehow suggested that it should work properly with GIFs. Perhaps some clarification need to be added in the doc.

skyline75489 commented 8 years ago

Currently it seems I can't get YYAnimatedImageView with YYWebImage. Is there any plan to support this feature?

I know GIFs consumes lots of CPU and memory. And (surprisingly) there's not a solution for GIF caching. FLAnimatedImage does not support caching yet. I'm using a FLAnimatedImage+YYCache as a temporary solution.

ibireme commented 8 years ago

YYImage will hold the original image data if the image contains multi-frame, it also support NSCoding for cache:

UIImageView *imageView = [YYAnimatedImageView new];
imageView.yy_imageURL = url;
/// ...
if ([imageView.image isKindOfClass:[YYImage class]]) {
    YYImage *image = (id)imageView.image;
    if (image.animatedImageType == YYImageTypeGIF) {
        NSData *gifData = image.animatedImageData;
    }
    if (image.animatedImageType == YYImageTypeWebP) {
        NSData *webpData = image.animatedImageData;
    }
}

/// ...
YYWebImageManager *manager = [YYWebImageManager sharedManager];
YYImageCache *cache = manager.cache;
UIImage *image = [cache getImageForKey:[manager cacheKeyForURL:url]];
if ([image isKindOfClass:[YYImage class]]) {
    YYImage *animatedImage = (id)image;
    NSData *animatedImageData = animatedImage.animatedImageData;
}
skyline75489 commented 8 years ago

I'm trying to avoid using both FLAnimatedImage and YYImage in the same project for the sake of consistency. The code above is basically doing the same thing as I did in FLAnimatedImage+YYCache.

If I were to use YYWebImage, I would prefer an unified API like what I made in FLAnimatedImage+YYCache, so that I can use the same API for GIF as other image format:

if ([cell isKindOfClass:[GifCollectionViewCell class]]) {
    [_cell.imageView yy_setImageWithURL:[self getImageURLForIndexPath:indexPath] progress:nil completed:nil];
}

Although the CPU and memory consumption is not changed, the API is much cleaner.

By the way, have you done any comparison between FLAnimatedImage and YYAnimatedImage? Which one is better at performance?

ibireme commented 8 years ago

You may add your custom setImage method, or fork this project...

YYAnimatedImage is inspired by FLAnimatedImage, so the performance is similar.

skyline75489 commented 8 years ago

OK, I'll close this issue. If others have the same needs they may found this issue helpful. Thanks for the clarification.