Why not cache images in YYFrameImage?
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.
Different imageViews may have different playback progress, so you may cache all frames in memory. You may [UIImage animatedImageWithImages:duration:] instead.
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. 👍