-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleGifImageView.m
More file actions
163 lines (130 loc) · 4.12 KB
/
SimpleGifImageView.m
File metadata and controls
163 lines (130 loc) · 4.12 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// SimpleGifImageView.m
// SimpleGifImageView
//
// Created by york <gyq5319920@gmail.com> on 3/19/14.
// Copyright (c) 2014 Truecolor.Inc. All rights reserved.
//
#import <ImageIO/ImageIO.h>
#import <mach/mach.h>
#import "SimpleGifImageView.h"
// the following three methods are copied and modified from project:
// https://github.com/arielelkin/DrakingSampler/blob/master/UIImage%2BanimatedGIF.m
static int delayCentisecondsForImageAtIndex(CGImageSourceRef const source, size_t const i) {
int delayCentiseconds = 1;
CFDictionaryRef const properties = CGImageSourceCopyPropertiesAtIndex(source, i, NULL);
if (properties) {
CFDictionaryRef const gifProperties = CFDictionaryGetValue(properties, kCGImagePropertyGIFDictionary);
if (gifProperties) {
CFNumberRef const unclampedDelayTime = CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFUnclampedDelayTime);
if (unclampedDelayTime) {
delayCentiseconds = (int)lrint([(__bridge id) unclampedDelayTime doubleValue] * 100);
}else{
CFNumberRef const number = CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFDelayTime);
delayCentiseconds = (int)lrint([(__bridge id) number doubleValue] * 100);
}
}
CFRelease(properties);
}
return delayCentiseconds;
}
static int pairGCD(int a, int b) {
if (a < b)
return pairGCD(b, a);
while (true) {
int const r = a % b;
if (r == 0)
return b;
a = b;
b = r;
}
}
static int vectorGCD(size_t const count, int const *const values) {
int gcd = values[0];
for (size_t i = 1; i < count; ++i) {
gcd = pairGCD(values[i], gcd);
}
return gcd;
}
@interface SimpleGifImageView() {
NSUInteger _currentFrameIndex;
NSTimer *_timer;
NSTimeInterval _timeInterval;
CGImageRef *_imageRefs;
NSUInteger _framesCount;
}
@end
@implementation SimpleGifImageView
- (void)dealloc {
[self releaseImageRefs];
#if !__has_feature(objc_arc)
[super dealloc];
#endif
}
- (void)setGifImageData:(NSData *)data {
[self releaseImageRefs];
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFTypeRef)data, nil);
size_t const count = CGImageSourceGetCount(source);
CGImageRef imageRefs[count];
int delayCentiseconds[count];
int totalDurationCentiseconds = 0;
for (size_t i = 0; i < count; ++i) {
imageRefs[i] = CGImageSourceCreateImageAtIndex(source, i, NULL);
delayCentiseconds[i] = delayCentisecondsForImageAtIndex(source, i);
totalDurationCentiseconds += delayCentiseconds[i];
}
int const gcd = vectorGCD(count, delayCentiseconds);
_framesCount = totalDurationCentiseconds / gcd;
_imageRefs = (CGImageRef *)calloc(_framesCount, sizeof(CGImageRef));
for (size_t i=0, k=0; i < count; ++i) {
_imageRefs[k++] = imageRefs[i];
for (size_t j = delayCentiseconds[i] / gcd; j > 1; --j) {
k++;
}
}
_timeInterval = gcd/100.0;
[self stopTimer];
[self startTimer];
CFRelease(source);
}
- (void)removeFromSuperview {
[self stopTimer];
[super removeFromSuperview];
}
- (void)releaseImageRefs {
for (int i=0; i<_framesCount; i++) {
if(_imageRefs[i]) CFRelease(_imageRefs[i]);
}
free(_imageRefs);
_imageRefs = nil;
}
- (void)stopTimer {
if (_timer) {
[_timer invalidate];
_timer = nil;
}
}
- (void)startTimer {
if (_framesCount <= 1) return;
_timer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval
target:self
selector:@selector(showNextFrame)
userInfo:nil
repeats:YES];
}
- (void)showNextFrame {
_currentFrameIndex ++;
if (_currentFrameIndex == _framesCount)
_currentFrameIndex = 0;
if (!_imageRefs[_currentFrameIndex]) return;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
if (!_imageRefs || !_imageRefs[_currentFrameIndex]) return;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1, -1);
CGContextDrawImage(context, rect, _imageRefs[_currentFrameIndex]);
}
@end