Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not cache images in YYFrameImage? #53

Open
wang9262 opened this issue Aug 15, 2016 · 2 comments
Open

Why not cache images in YYFrameImage? #53

wang9262 opened this issue Aug 15, 2016 · 2 comments

Comments

@wang9262
Copy link

Imagine this scenario:
I have some imageViews that init with the same frame image( instance of YYFrameImage),and add them to some view simultaneously.Apparently,the more imageViews,the slower animations,because of high CPU usage to decode images.So is adding an array in YYFrameImage to cache the decoded images a correct way like the code below?

@implementation YYFrameImage {
    NSUInteger _loopCount;
    NSUInteger _oneFrameBytes;
    NSArray *_imagePaths;
    NSArray *_imageDatas;
    NSArray *_frameDurations;
    NSMutableArray *_decodedImages;  // to cache the decoded images
}

- (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index {
    // suppose we init this array before
    if (index < _decodedImages.count) {
        return _decodedImages[index];
    }
    if (_imagePaths) {
        if (index >= _imagePaths.count) return nil;
        NSString *path = _imagePaths[index];
        CGFloat scale = _NSStringPathScale(path);
        NSData *data = [NSData dataWithContentsOfFile:path];
        UIImage *image = [[UIImage imageWithData:data scale:scale] yy_imageByDecoded];
        // cache decoded image
        [_decodedImages addObject:image];
        return image;
    } else if (_imageDatas) {
        if (index >= _imageDatas.count) return nil;
        NSData *data = _imageDatas[index];
        UIImage *image = [[UIImage imageWithData:data scale:[UIScreen mainScreen].scale] yy_imageByDecoded];
        // cache decoded image
        [_decodedImages addObject:image];
        return image;
    } else {
        return index == 0 ? self : nil;
    }
}

And we can also add some methods to handle the memory warning and background mode.

@ibireme
Copy link
Owner

ibireme commented Aug 16, 2016

Different imageViews may have different playback progress, so you may cache all frames in memory. You may [UIImage animatedImageWithImages:duration:] instead.

@wang9262
Copy link
Author

OK,I konw.Maybe in my scenario,it's better to use a cached images because there may be many imageViews with the same frameImage showing at the same time.If still using your implementation will cause very high CPU usage (that's to say average 90%).I will subclass UIImage and conforms to <YYAnimatedImage> to cache frame images.
What's more,there are other features like loop mode(repeat or reverse) and loop range (some frame to another not start to end).I will give a pull request if these feature described above are suitable for this framework.
Thanks for your reply and this great framework. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants