-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQHQTabBarItemGifView.m
64 lines (52 loc) · 1.77 KB
/
QHQTabBarItemGifView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// QHQTabBarItemGifView.m
//
// Created by lujianwen on 2018/11/26.
// Copyright © 2018 All rights reserved.
//
#import "QHQTabBarItemGifView.h"
#import <FLAnimatedImageView+WebCache.h>
#import "QHQResourceManager.h"
@interface QHQTabBarItemGifView ()
@property (nonatomic, strong) FLAnimatedImageView *imageView;
@end
@implementation QHQTabBarItemGifView
- (void)setupUI {
[super setupUI];
_imageView = [[FLAnimatedImageView alloc] init];
[self.contentView addSubview:_imageView];
}
- (void)setContent:(id)content {
[self setupImageViewWith:(NSData *)content];
}
- (void)setIsSelected:(BOOL)isSelected {
[super setIsSelected:isSelected];
if (isSelected) {
[_imageView startAnimating];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
_imageView.frame = self.contentView.bounds;
}
- (void)setupImageViewWith:(NSData *)imageData {
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, nil);
NSInteger frameCount = CGImageSourceGetCount(imageSource);
float duration =0;
NSMutableArray *images = [NSMutableArray array];
for(int i =0; i < frameCount; i ++) {
CGImageRef cgImage = CGImageSourceCreateImageAtIndex(imageSource, i, nil);
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, i, nil);
NSDictionary *gifInfo = (__bridge NSDictionary*)properties;
duration = [gifInfo[@"{GIF}"][@"DelayTime"] doubleValue] + duration;
UIImage *image = [UIImage imageWithCGImage:cgImage];
[images addObject:image];
if (i == frameCount - 1) {
_imageView.image = image;
}
}
_imageView.animationImages = images;
_imageView.animationDuration = duration;
_imageView.animationRepeatCount = 1;
}
@end