参考:
SDWebimage解析网络动画图片的过程
1、将data转换成img
NSData *data = xxx;
UIImage *img = SDImageCacheDecodeImageData(data, @"", SDWebImageContinueInBackground, @{});
如果是动画的图,内部解析并转换成UIImage
animatedImage = [UIImage animatedImageWithImages:animatedImages duration:totalDuration / 1000.f];
SDWebimage使用本地图片展示动画图的过程
SDAnimatedImageView+SDAnimatedImage
YYWebImage使用本地图片展示动画的过程
YYAnimatedImageView+YYImage
默认让UIImageView支持YYImage的动画播放的原理就是hook掉UIImageView的setImage方法,如果YYImage里有多帧,将YYImage转换成UIImage来进行展示
#import "UIImageView+YYAutoAnim.h"
#import <objc/runtime.h>
#import "YYAnimatedImageView.h"
#import "YYImage.h"
@implementation UIImageView (YYAutoAnim)
+(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class clazz = objc_getClass("UIImageView")
SEL sel1 = @selector(setImage:) ;
SEL sel2 = @selector(yy_Anim_setImage:) ;
Method origMethod = class_getInstanceMethod(clazz, sel1);
Method altMethod = class_getInstanceMethod(clazz, sel2);
BOOL didAddMethod = class_addMethod(clazz,sel1,
method_getImplementation(altMethod),
method_getTypeEncoding(altMethod));
if (didAddMethod) {
class_replaceMethod(clazz,altSel,
method_getImplementation(origMethod),
method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, altMethod);
}
});
}
- (void)yy_Anim_setImage:(UIImage *)image {
if ([image isKindOfClass:YYImage.class] && ![self isKindOfClass:YYAnimatedImageView.class]) {
YYImage *yImage = (YYImage *)image;
if (yImage.animatedImageFrameCount<=1) {
[self yy_Anim_setImage:image];
}else{
NSMutableArray *imageList = [NSMutableArray array];
NSTimeInterval duration = 0;
for (NSInteger i=0; i<yImage.animatedImageFrameCount; i++) {
[imageList addObject:[yImage animatedImageFrameAtIndex:i]];
duration += [yImage animatedImageDurationAtIndex:i];
}
UIImage *animatedImg = [UIImage animatedImageWithImages:imageList duration:duration];
[self yy_Anim_setImage:animatedImg];
}
}else{
[self yy_Anim_setImage:image];
}
}
@end
网友评论